java.util.Date – Parse, format and conversion examples

Learn to create new date, get current date, parse date to string or format Date object using java.util.Date class. These use-cases are frequently required, and having them in one place will help in saving time for many of us.

java date

Table of Contents

Format Date to string
Parse string to Date
Get current date
Get current time
Convert Calendar to Date
Get Date to Calendar
Compare between two dates
Get date parts (year, month, day of month)

Format Date to String

Example of formatting Date to String representation.

SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy");
String date = sdf.format(new Date()); 
System.out.println(date); //Prints 26/10/2015

Refer SimpleDateFormat JavaDoc for detail date and time patterns. Below is a list of the most common pattern letters you can use.

y   = year   (yy or yyyy)
M   = month  (MM)
d   = day in month (dd)
h   = hour (0-12)  (hh)
H   = hour (0-23)  (HH)
m   = minute in hour (mm)
s   = seconds (ss)
S   = milliseconds (SSS)
z   = time zone  text        (e.g. Pacific Standard Time...)
Z   = time zone, time offset (e.g. -0800)

For example,

yyyy-MM-dd           	(2015-12-31)
dd-MM-YYYY           		(31-10-2015)
yyyy-MM-dd HH:mm:ss  		(2015-12-31 23:59:59)
HH:mm:ss.SSS         		(23:59.59.999)
yyyy-MM-dd HH:mm:ss.SSS   	(2015-12-31 23:59:59.999)
yyyy-MM-dd HH:mm:ss.SSS Z   (2015-12-31 23:59:59.999 +0100)

Parse String to Date

Example of parsing String to Date object.

SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
String dateInString = "15-10-2015 10:20:56";
Date date = sdf.parse(dateInString);
System.out.println(date); //Prints Tue Oct 15 10:20:56 SGT 2015

Get current date

Example of getting current date in Java.

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date)); //2015/10/26 12:10:39

In java 8, you can use LocalDate class.

LocalDate today = LocalDate.now();
System.out.println("Today's Local date : " + today);

Get Current Time

Example of getting current time in Java.

LocalTime time = LocalTime.now();
System.out.println("local time now : " + time);

Convert Calendar to Date

Calendar calendar = Calendar.getInstance();
Date date =  calendar.getTime();

Convert Date to Calendar

SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
String dateInString = "27-04-2016 10:22:56";
Date date = sdf.parse(dateInString);

Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

Compare between two Dates

Example of comparing two date instances.

Date date1 = new Date();
Date date2 = new Date();

int comparison = date1.compareTo(date2);

The comparison between dates follows the rules for the Comparable interface, meaning the compareTo() method returns:

  1. An int larger than 0 if the date the method is called on is later than the date given as parameter.
  2. An int value of 0 if the dates are equal.
  3. An int value less than 0 if the date the method is called on is earlier than the date given as parameter.

Since java 8, LocalDate class has overridden equals() method to provide date equality.

LocalDate today = LocalDate.now();
LocalDate date1 = LocalDate.of(2015, 10, 26);
if(date1.equals(today)){
    System.out.printf("Today %s and date1 %s are same date %n", today, date1);
}

Get date parts (year, month, day of month)

Example of getting date parts such as year, month, etc separately.

The methods to get the year, month, day of the month, hour, etc. are deprecated. If you need to get or set the year, month, day of the month, etc. use a java.util.Calendar instead.

Calendar calendar = new GregorianCalendar();

int year       = calendar.get(Calendar.YEAR);
int month      = calendar.get(Calendar.MONTH); 
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); // Jan = 0, not 1
int dayOfWeek  = calendar.get(Calendar.DAY_OF_WEEK);
int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
int weekOfMonth= calendar.get(Calendar.WEEK_OF_MONTH);

int hour       = calendar.get(Calendar.HOUR);        // 12 hour clock
int hourOfDay  = calendar.get(Calendar.HOUR_OF_DAY); // 24 hour clock
int minute     = calendar.get(Calendar.MINUTE);
int second     = calendar.get(Calendar.SECOND);
int millisecond= calendar.get(Calendar.MILLISECOND);

FYI, in java 8, you can get different time units in the below fashion.

LocalDate today = LocalDate.now();
int year = today.getYear();
int month = today.getMonthValue();
int day = today.getDayOfMonth();

That’s all for these Java 7 Date examples.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

2 thoughts on “java.util.Date – Parse, format and conversion examples”

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.