在java中将GMT转换为IST? [英] Convert GMT to IST in java?

查看:103
本文介绍了在java中将GMT转换为IST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GMT字段,其中用户输入要转换为IST的时间(例如:在小时字段18,分钟字段30,在会话字段am / pm中)。我需要获取这些输入并在java中转换为IST ???

I have a GMT field in which the user enter a time to be converted to IST (for eg: in hour field 18, minute field 30, in session field am/pm). I need to get those inputs and convert to IST in java???

推荐答案

如果你意识到这一点,这是非常容易和明显的时区仅与格式化为String的日期相关 - 秒/毫秒时间戳(其中 java.util.Date 仅仅是包装器)始终是隐式UTC(GMT是什么)适当地叫)。在这样的时间戳和字符串之间进行转换总是使用时区。

This is very easy and obvious if you realize that the timezone is only relevant for a date formatted as String - second/millisecond timestamps (of which java.util.Date is merely a wrapper) are always implicitly UTC (what GMT is properly called). And converting between such a timestamp and a string always uses a timezone, both ways.

所以这就是你需要做的事情:

So this is 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);

这篇关于在java中将GMT转换为IST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆