Convert GMT to ist in Java?

I have a GMT field where the user enters the time to convert to IST (for example, in the hour field 18, the minute field 30, and the session field AM / PM) I need to get these inputs and convert them into ist in Java???

Solution

If you realize that the time zone is only related to a date formatted as a string, then this is very straightforward – the second / millisecond timestamp (where java.util.date is just a wrapper) is always implicit UTC (GMT is called correctly) The time zone is always used to convert between such timestamp and string

So that's what you need to do:

DateFormat utcFormat = new SimpleDateFormat(patternString);
    utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    DateFormat indianFormat = new SimpleDateFormat(patternString);
    utcFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    Date timestamp = utcFormat.parse(inputString);
    String output = indianFormat.format(timestamp);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>