DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Convert a Date Object to a Java String

Convert a Date Object to a Java String

This quick tutorial will walk you through how to convert a date object into a string.

Orlando Mabry user avatar by
Orlando Mabry
·
Nov. 29, 16 · Tutorial
Like (2)
Save
Tweet
Share
12.86K Views

Join the DZone community and get the full member experience.

Join For Free

In Java, the java.util.Date class represents a date and time. This class has a number of methods to set and retrieve the various parts that make up a date and time (day, month, hour, etc.), but these methods are deprecated, and the recommended way to set a given field is by using another class: java.util.Calendar. This is an abstract class, and is usually instantiated by calling its getInstance() static method, which returns an object initialized with the current date and time:

Calendar cal = Calendar.getInstance();

In the Calendar class, each field is represented by a static class member (for example, Calendar.YEAR, Calendar.DAY_OF_MONTH, etc.), and the set(int field, int value) method can be used to set a value for a given field: for example, the following code snippet sets the year to 2016:

cal.set(Calendar.YEAR, 2016);

Once all relevant date and time fields have been set, a Date object can be created with the getTime() method:

Date dateObject = cal.getTime();

In order to convert a Date object to a Java string (for example, to print it in a human-readable form), the java.text.SimpleDateFormat class can be used; first, this class is instantiated by specifying the format in which the date and time needs to be printed and the locale setting, then its format() method is called, passing it the Date object containing the date and time:

SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.ENGLISH);
String dateString = format.format(dateObject);


An example string obtainable with the above instantiation of the SimpleDateFromat class is "2016.07.14 19:58:06".

Object (computer science) Java (programming language) Strings Data Types Convert (command)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • API Design Patterns Review
  • Microservices Discovery With Eureka
  • 7 Awesome Libraries for Java Unit and Integration Testing
  • What Is a Kubernetes CI/CD Pipeline?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: