How to compare dates from two different timezones in Java

Here is an example that compares two dates and time from two different time zones.

Syntax:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm aa");
simpleDateFormat.setTimeZone( TimeZone.getTimeZone("America/New_York") );
try {
	newYorkDate = simpleDateFormat.parse( timeNewYork );
} catch (ParseException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

The code first creates a dateformatter using the pattern of the Dates. The formatter will be used to generate the date objects using the date Strings. The time zone is set in the formatter to the Eastern time zone to parse the New York date that is in the eastern time zone. Then, the time zone is set to the Pacific Time zone, in order to parse the Los Angeles time and create the laDate object.
Finally, the 2 Date Objects are compared using the before method. 2 PM eastern time is 7 PM GMT time and 10 AM pacific time is 6 PM in the GMT timezone.

1- Create the following java file:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class DateTimeZoneCompare {

	public static void main(String[] argv){
		
		String pattern = "yyyy-MM-dd hh:mm aa";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

		// New York Time
		String timeNewYork = "2017-01-19 02:00 PM";

		// Time in LA
		String timeLA      = "2017-01-19 10:00 AM";

		Date newYorkDate = null;
		Date laDate = null;

		// Set the time zone to eastern time 
		simpleDateFormat.setTimeZone( TimeZone.getTimeZone("America/New_York") );
		try {
			newYorkDate = simpleDateFormat.parse( timeNewYork );
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// Set the time zone to pacific time 
		simpleDateFormat.setTimeZone( TimeZone.getTimeZone("America/Los_Angeles") );
		try {
			laDate = simpleDateFormat.parse( timeLA );
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// Set the time zone to GMT, that has the date
		simpleDateFormat.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
		
		System.out.println( "Time in New York eatern time    : " + timeNewYork );
		System.out.println( "Time in Los Angeles pacific time: " + timeLA );

		System.out.println( "Time in New York    GMT: " + simpleDateFormat.format( newYorkDate ) );
		System.out.println( "Time in Los Angeles GMT: " + simpleDateFormat.format( laDate ) );
		
		if ( laDate.before( newYorkDate ) )
			System.out.println( "timeLA is before newYorkDate" );
		else
			System.out.println( "timeLA is after newYorkDate" ); 
		
	}
	
}
	

The output will be:

Time in New York eatern time    : 2017-01-19 02:00 PM
Time in Los Angeles pacific time: 2017-01-19 10:00 AM
Time in New York    GMT: 2017-01-19 07:00 PM
Time in Los Angeles GMT: 2017-01-19 06:00 PM
timeLA is before newYorkDate

Java stores internally Date in the GMT timezone. When creating a date Object it is preferable to use a specific timezone, this way no default value is used.

References:

java Date

Share this article:

Recent Comments