Convert GMT to IST in java?

前端 未结 4 612
旧巷少年郎
旧巷少年郎 2021-01-21 11:12

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 conv

4条回答
  •  青春惊慌失措
    2021-01-21 11:45

    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);
        indianFormat .setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
        Date timestamp = utcFormat.parse(inputString);
        String output = indianFormat.format(timestamp);
    

提交回复
热议问题