Compare two Dates in Java | Java SimpleDateFormat - Java Date Format

Jyotishgher Astrology
By -
0

 

Java SimpleDateFormat - Java Date Format

dd-MMM-yy(14-NOV-22)

Introduction

Java SimpleDateFormat and DateFormat classes are used for date formatting. It is mostly used where we need to display or utilize the date and time functionality of Java. Both of these classes are present in com.text package.

Java DateFormat

  • DateFormat is used for formatting a date into String based on specific locale that is provided as input.
  • The locale is used for specifying the region and language for making the code more locale to the user.
  • The way of writing date is different in different regions of the world. For example, 31st Dec 2017 will be written in India as 31-12-2017 but the same thing will be written in the United States of America as 12-31-2017.


Patterns

Let us have a look at the pattern syntax that should be used for the formatting pattern.

Letter for PatternDate or Time componentExamples
GEra designatorAD
yYear2018 (yyyy), 18 (yy)
MMonth in yearJuly (MMMM), Jul (MMM), 07 (MM)
wResults in week in year16
WResults in week in month3
DGives the day count in the year266
dDay of the month09 (dd), 9(d)
FDay of the week in month4
EDay name in the weekTuesday, Tue
uDay number of week where 1 represents Monday, 2 represents Tuesday and so on2
aAM or PM markerAM
HHour in the day (0-23)12
kHour in the day (1-24)23
KHour in am/pm for 12 hour format (0-11)0
hHour in am/pm for 12 hour format (1-12)12
mMinute in the hour59
sSecond in the minute35
SMillisecond in the minute978
zTimezonePacific Standard Time; PST; GMT-08:00
ZTimezone offset in hours (RFC pattern)-0800
XTimezone offset in ISO format-08; -0800; -08:00

Some letters should be used in different amount for different results like for month:

TypePatternExample Output
Full MonthMMMMJuly
Abbreviated MonthMMMJul
Numeric MonthMM07

Examples

Let us now look at some examples for different formats of date and time.

PatternResult
MM/dd/yyyy01/02/2018
dd-M-yyyy hh:mm:ss02-1-2018 06:07:59
dd MMMM yyyy02 January 2018
dd MMMM yyyy zzzz02 January 2018 India Standard Time
E, dd MMM yyyy HH:mm:ss zTue, 02 Jan 2018 18:07:59 IST

import java.text.*;

import java.util.Date;

public class CompareTwoDatesTest {

   public static void main(String[] args) throws ParseException {

      SimpleDateFormat sdformat = new SimpleDateFormat("dd-MMM-yy");

      Date d1 = sdformat.parse("14-NOV-22");

      Date d2 = sdformat.parse("12-NOV-22");

      System.out.println("The date 1 is: " + sdformat.format(d1));

      System.out.println("The date 2 is: " + sdformat.format(d2));

      if(d1.compareTo(d2) > 0) {

         System.out.println("Date 1 occurs after Date 2");

      } else if(d1.compareTo(d2) < 0) {

         System.out.println("Date 1 occurs before Date 2");

      } else if(d1.compareTo(d2) == 0) {

         System.out.println("Both dates are equal");

      }

   }

}

Post a Comment

0Comments

Post a Comment (0)