Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current time in your timezone in Java

I have no idea why Java makes it near impossible to do such a simple thing. I have tried many solutions I find online, but there doesn't seem to be a simple clean and working solution.

Here is my latest attempt at a solution

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 excepted output: 27/02/2014 17:06:00

My real output: 03/03/2014 20:35:44

How does this even make any sense.

like image 251
user2158382 Avatar asked Nov 24 '25 23:11

user2158382


1 Answers

set timezone in SimpleDateFormat instance instead

sdf.setTimezone("America/Los_Angeles");
like image 173
jmj Avatar answered Nov 26 '25 11:11

jmj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!