convert from one timezone to another in java

I have a datetime in UTC timezone in UTC format ("yyyy-MM-dd'T'HH:mm:ss'Z'). Is there an easy way (without using joda) to convert it into another timezone (PDT,EST etc) in a certain/or standard (to that timezone) date format?

Thanks

Answers


public String formatInTimezone(String utcDateString, String intendedTimeZone) {
    SimpleDateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date theDate = utcFormat.parse(utcDateString);

    SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss z");
    outputFormat.setTimeZone(TimeZone.getTimeZone(intendedTimeZone));
    return outputFormat.format(theDate);
}

You can look into using date4j instead of joda (though I would prefer joda unless there were clear reasons not to use it).

DateTime dt = DateTime.now(someTimeZone);
dt.changeTimeZone(fromOneTimeZone, toAnotherTimeZone);

Perhaps something like this:

DateFormat formatter= new SimpleDateFormat("MM/dd/yyyy hh:mm:ss Z");
  formatter.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
  System.out.println(formatter.format(date));

  formatter.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));
  System.out.println(formatter.format(instance2.getTime()))

Create a calendar object and set it time based on the local instead of germany use EST and PDT

Calendar localTime = Calendar.getInstance();
    localTime.set(Calendar.HOUR, 17);
    localTime.set(Calendar.MINUTE, 15);
    localTime.set(Calendar.SECOND, 20);

int hour = localTime.get(Calendar.HOUR);
int minute = localTime.get(Calendar.MINUTE);
int second = localTime.get(Calendar.SECOND);


// Print the local time
System.out.printf("Local time  : %02d:%02d:%02d\n", hour, minute, second);


// Create a calendar object for representing a Germany time zone. Then we
// wet the time of the calendar with the value of the local time

Calendar germanyTime = new GregorianCalendar(TimeZone.getTimeZone("Germany"));
germanyTime.setTimeInMillis(localTime.getTimeInMillis());
hour = germanyTime.get(Calendar.HOUR);
minute = germanyTime.get(Calendar.MINUTE);
second = germanyTime.get(Calendar.SECOND);

    // Print the local time in Germany time zone
    System.out.printf("Germany time: %02d:%02d:%02d\n", hour, minute, second);

Need Your Help

Menu glitchy on top of slideshow

javascript css slideshow onmouseover

I have implemented the following set up (after being requested):

Redraw balloon tip and tooltip in C#?

c# tooltip redraw balloon

I wrote a C# application that is a simple countdown timer. I use it myself to keep track of cooking time (not to forget about boiling pasta) and other purposes.