Java 7 Date – How to Parse and Format

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.

1. Formatting a Date

Java program of formatting Date to string using SimpleDateFormat. Please note that SimpleDateFormat is not a thread-safe class, so we should not share its instance with multiple threads.

SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy");

String date = sdf.format(new Date());

Refer SimpleDateFormat JavaDoc for detailed 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)
Pattern
Example
yyyy-MM-dd (ISO)
“2018-07-14”
dd-MMM-yyyy
“14-Jul-2018”
dd/MM/yyyy
“14/07/2018”
E, MMM dd yyyy
“Sat, Jul 14 2018”
h:mm a
“12:08 PM”
EEEE, MMM dd, yyyy HH:mm:ss a
“Saturday, Jul 14, 2018 14:31:06 PM”
yyyy-MM-dd'T'HH:mm:ssZ
“2018-07-14T14:31:30+0530”
hh 'o''clock' a, zzzz
“12 o’clock PM, Pacific Daylight Time”
K:mm a, z
“0:08 PM, PDT”

2. Parsing a String to Date

Java program of parsing a string to Date instance.

SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");

String dateInString = "15-10-2015 10:20:56";

Date date = sdf.parse(dateInString);

3. Current Date and Time

java.util.Date class represents the date and time in Java 7. Given below are the Java programs for getting the current date and time and printing in a given format.

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

For reference, in java 8, we can use LocalDate class.

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

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

4. Date and Time Conversions

4.1. Converting Calendar to Date

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

4.2. Converting 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);

5. Comparing Two Dates

Java program to compare two Date instances using the compareTo() method.

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

int comparison = date1.compareTo(date2);

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 check 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);
}

6. Extracting Days, Months and Years

Java program to get 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, we 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+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

3 thoughts on “Java 7 Date – How to Parse and Format”

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.