Java – How to get the current time in your time zone in Java

How to get the current time in your time zone in Java… here is a solution to the problem.

How to get the current time in your time zone in Java

I don’t know why Java is almost impossible to do such a simple thing. I’ve tried a lot of solutions I’ve found online, but there doesn’t seem to be a simple, clean and effective solution.

Here is my recent solution attempt

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles"));
System.out.println( sdf.format(calendar.getTime()) );

My exception output: 27/

02/2014 17:06:00

My real output: 03/

03/2014 20:35:44

What’s the point of that.

Solution

Set the time zone in the SimpleDateFormat instance instead

sdf.setTimezone("America/Los_Angeles");

Related Problems and Solutions