Working with various Calendar TimeZone in Java (without using Joda Time)

前端 未结 2 795
臣服心动
臣服心动 2021-01-25 01:18

I was looking for a way to get current time in various timezones based on an user input. I know I could use Joda Time! but is that the only way?

Isn\'t there an option

2条回答
  •  天涯浪人
    2021-01-25 01:48

    As Jon pointed out the method getTime() is returning a java.util.Date object which is just a millisecond value and not timezone aware.

    If you are just looking at printing the times then you can use the calendar and manually get the fields you want like

    System.out.println(utc.get(Calendar.HOUR_OF_DAY) + ":" + utc.get(Calendar.MINUTE))
    

    This would need some formatting for a minute < 10 to display the 0

提交回复
热议问题