Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook | Java 11 Cookbook
Mohamed Sanaulla wrote:Have a look at JavaDatesFaq
Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook | Java 11 Cookbook
Jesper de Jong wrote:The first thing to understand is that class Date does not know anything about timezones. So, if you parse a string that contains a timezone indication into a Date object, then that Date object will not remember what the timezone was in the string. A Date object is nothing more than an absolute point in time, independent of the timezone.
When you want to format a date so that the string will contain a specific timezone, you must set the timezone on the DateFormat object before formatting it. For example:
Ashu Bharadwaj wrote:... we are not displaying the parsed date string. This line of code adjusts time according to the offset and displays in GMT. In my case reduces 5.30 hr.
Jesper de Jong wrote:
Ashu Bharadwaj wrote:... we are not displaying the parsed date string. This line of code adjusts time according to the offset and displays in GMT. In my case reduces 5.30 hr.
Ofcourse 5:30 is subtracted, because when it is 10:00 o'clock in Great Britain (GMT timezone), it is 15:30 in India (IST timezone) - there is a time difference of 5 hours and 30 minutes. You would have to subtract 5 hours and 30 minutes from the Date object, which you can do with a Calendar object - it contains methods to do calculations with dates and times.
But why exactly do you want to do that? Can you explain what your exact goal is with your code, so that we can help you find a good solution?
Ashu Bharadwaj wrote:The returned Date object should display GMT when we display using System.out.println(Date object);
Water! People swim in water! Even tiny ads swim in water:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|