This discussion is archived
7 Replies Latest reply: Apr 23, 2013 2:33 AM by Tolls RSS

GregorianCalendar - UTC to local standard time

802569 Newbie
Currently Being Moderated
Hi, I need some help with calendar manipulation. I have an GregorianCalendar in UTC format and I need to convert it to local standard time, ignoring DST. So for example, I have 2013-04-01T06:00:00.0Z and need to convert it to eastern standard time (e.g. 2013-04-01T01:00:00-05:00) regardless of what time of year it is. Is it possible to specify that a timezone be in standard time only?

thanks
  • 1. Re: GregorianCalendar - UTC to local standard time
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    799566 wrote:
    Hi, I need some help with calendar manipulation. I have an GregorianCalendar in UTC format
    No you don't.
    There is no "format" in the sense that you use it in the rest of your question.

    Presumably you set the timezone in some fashion and that impacts how it parses the input values that you give it. It doesn't change how the data is kept.
    and I need to convert it to local standard time, ignoring DST.
    Not possible. The data in the GregorianCalendar is UTC. Always. There is nothing that you do that changes that.
    So for example, I have 2013-04-01T06:00:00.0Z and need to convert it to eastern standard time (e.g. 2013-04-01T01:00:00-05:00) regardless of what time of year it is. Is it possible to specify that a timezone be in standard time only?
    What you have is a GregorianCalendar with data which represents the former.
    And what you need to do is display the later. So you do the following
    - Use getTime() to get a java.util.Date.
    - Use java.txt.SimpleDateFormat, set the values appropriately, the use format() to create the display value.
  • 2. Re: GregorianCalendar - UTC to local standard time
    KonradZuse Explorer
    Currently Being Moderated
    I haven't checked it completely, but you should be able to complete everything oyu need using http://download.java.net/jdk8/docs/api/java/time/package-summary.html Is the new Time package for Java 8, so if you want to try out a beta build I recommend this time and it's formatters http://download.java.net/jdk8/docs/api/java/time/format/package-summary.html over the annoying Calendar... BTW DATE has been depreciated since what? 1.1?
  • 3. Re: GregorianCalendar - UTC to local standard time
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    KonradZuse wrote:
    I haven't checked it completely, but you should be able to complete everything oyu need using http://download.java.net/jdk8/docs/api/java/time/package-summary.html Is the new Time package for Java 8, so if you want to try out a beta build I recommend this time and it's formatters http://download.java.net/jdk8/docs/api/java/time/format/package-summary.html over the annoying Calendar...
    Naturally since it is in fact a beta and it is a new class one might suppose that one better check it out very carefully first.
    BTW DATE has been depreciated since what? 1.1?
    No idea what that is referring to but java.util.Date is not deprecated.
  • 4. Re: GregorianCalendar - UTC to local standard time
    802569 Newbie
    Currently Being Moderated
    Thanks for help. Need to stick to Java 6 standards though.
    What you have is a GregorianCalendar with data which represents the former.
    And what you need to do is display the later. So you do the following
    - Use getTime() to get a java.util.Date.
    - Use java.txt.SimpleDateFormat, set the values appropriately, the use format() to create the display value.
    If I understand correctly, I would have to manipulate the Date with the raw offset to standard time, then format with SimpleDateFormat without using a timezone component, instead adding the timezone (i.e. '-05:00') piece myself? Is that what you had in mind?

    thanks
  • 5. Re: GregorianCalendar - UTC to local standard time
    DrClap Expert
    Currently Being Moderated
    799566 wrote:
    Thanks for help. Need to stick to Java 6 standards though.
    What you have is a GregorianCalendar with data which represents the former.
    And what you need to do is display the later. So you do the following
    - Use getTime() to get a java.util.Date.
    - Use java.txt.SimpleDateFormat, set the values appropriately, the use format() to create the display value.
    If I understand correctly, I would have to manipulate the Date with the raw offset to standard time, then format with SimpleDateFormat without using a timezone component, instead adding the timezone (i.e. '-05:00') piece myself? Is that what you had in mind?
    The first step was "Use getTime() to get a java.util.Date". I don't see anything in there about manipulating it with a raw offset so I can't imagine where you got that idea from.

    The second step means to create a SimpleDateFormat object and assign it the time zone you want to display the date in. In your case you'd have to find a time zone which was GMT -5 with no DST. (There is one -- you might want to spend some time messing about with the TimeZone class to see what it does and what time zones are available.)
  • 6. Re: GregorianCalendar - UTC to local standard time
    802569 Newbie
    Currently Being Moderated
    The first step was "Use getTime() to get a java.util.Date". I don't see anything in there about manipulating it with a raw offset so I can't imagine where you got that idea from.

    The second step means to create a SimpleDateFormat object and assign it the time zone you want to display the date in. In your case you'd have to find a time zone which was GMT -5 with no DST. (There is one -- you might want to spend some time messing about with the TimeZone class to see what it does and what time zones are available.)
    I don't know which timezone is hosting the application, which is why I was using the raw offset to change the time. I don't see a way to figure out which timezone ID represents the local standard time.
  • 7. Re: GregorianCalendar - UTC to local standard time
    Tolls Journeyer
    Currently Being Moderated
    799566 wrote:
    The first step was "Use getTime() to get a java.util.Date". I don't see anything in there about manipulating it with a raw offset so I can't imagine where you got that idea from.

    The second step means to create a SimpleDateFormat object and assign it the time zone you want to display the date in. In your case you'd have to find a time zone which was GMT -5 with no DST. (There is one -- you might want to spend some time messing about with the TimeZone class to see what it does and what time zones are available.)
    I don't know which timezone is hosting the application, which is why I was using the raw offset to change the time. I don't see a way to figure out which timezone ID represents the local standard time.
    You don't need to.
    The Date returned by getTime() on Calendar is millis from the epoch, so is UTC.
    No need to do anything to it.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points