How to Display a date depending on timezone

This tutorial shows how to display a date depending on the timezone in Java.

Java uses the SimpleDateFormat to format dates.

Create the following java file:

package com.ussalaries.sites.oliviertech;

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

public class DateTimeZone {

	public static void main(String[] argv){
		
		String pattern = "yyyy-MM-dd hh:mm aa";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		
		Date now = new Date();
		
		simpleDateFormat.setTimeZone( TimeZone.getTimeZone("America/New_York") );
		System.out.println( "Time in New York:    " + simpleDateFormat.format( now ) );
		
		simpleDateFormat.setTimeZone( TimeZone.getTimeZone("America/Los_Angeles") );
		System.out.println( "Time in Los Angeles: " + simpleDateFormat.format( now ) ); 

	}
	
}


The output will be:

	
Time in New York:    2016-10-05 06:44 PM
Time in Los Angeles: 2016-10-05 03:44 PM
	

References:

SimpleDateFormat

Recent Comments