Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8701079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:18:33+00:00 2026-06-13T02:18:33+00:00

I am trying to format a calendar string to indicate a time zone offset

  • 0

I am trying to format a calendar string to indicate a time zone offset other than my local one. I am aware I could create a simple formatting string and use the Calendar.get(int) method to fill in all the values, but this does not feel like the right way to do this.

Java has a DateFormat, specifically I am trying to use the SimpleDateFormat. The problem is that the format() method of these classes expects a Date object.

I am primarily working with Calendar objects since I believe those are the recommended structure in Java. So, when I go to format my result time, I call Calendar.getTime() which returns a Date object which can be passed into the SimpleDateFormat object.

Until now, I thought this was perfectly simple, but here is where the problem comes in.

Whenever one calls the Calendar.getTime() method, the Date returned is always in the local time zone, regardless of the time zone of the Calendar.

So, I always get the time printed in the local time zone when I pass it to my SimpleDateFormat, which is not what I want. All the research I have done so far says it can’t be done and all the examples I have seen simply use the Calendar.get(int) method to fill in some blanks. This seems terrible, why have a DateFormat class if it is going to be so useless?

Here is some example code so you can see what I mean, paste this into your favourite test class:

private static final SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

public static void main(String[] args)
{
    try
    {
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("EST"));
        cal.setTimeInMillis(parser.parse("2012-10-09T22:01:49.108-0700").getTime());
        System.out.println(formatter.format(cal.getTime()));
    }
    catch(ParseException e)
    {
        e.printStackTrace();
    }
}

Output produced (because I am running in Central Time Zone): 2012-10-10T00:01:49-0500
Output expected (should not matter what time zone it is run from): 2012-10-10T01:01:49-0400

To summarize question: Is there a way to make the DateFormat in java accept a Calendar, or a way to get a Date that is not in the local timezone, or is there another class I should be using altogether for this formatting?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:18:34+00:00Added an answer on June 13, 2026 at 2:18 am

    Whenever one calls the Calendar.getTime() method, the Date returned is always in the local time zone, regardless of the time zone of the Calendar.

    No, it’s not. Date doesn’t have a time zone. It’s just the number of milliseconds since the Unix epoch. Its toString() method does convert it to the local time zone, but that’s not part of the information in the Date object.

    All you need to do is set the time zone of the formatter to be the same as the time zone of the calendar.

    formatter.setTimeZone(calendar.getTimeZone());
    

    … or just set the calendar to be used entirely:

    formatter.setCalendar(calendar);
    

    (It’s not immediately clear to me whether the latter approach will mean that the calendar can lose its value… basically the Java classes mix “calendar system”, “time zone” and “value within the calendar” in a single type, which is very unfortunate.)

    I agree with Ian though, in terms of Joda Time being a far more pleasant API to use.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When trying to get a string for the current date using DateFormat.getDateInstance().format(calendar.getTime()) it keeps
I am trying to get Data Type (Body Format) of Mail,Calendar e.t.c. Body. Getting
I'm trying to convert SimpleDateFormat to String, but result is not correct. Calendar cal
I'm trying to compare two millisecond values in Java. One from a calendar, and
I was trying to format a string into date. For this I have written
I am trying to convert a string into a DateTime and change its format
I am trying to compare a date in a String format to the current
I'm trying to get a String of a date in Java in the format
I have been hunting for ages trying to determine what format of calendar invite/event
I am trying to make Calendar month&year picker. How can I set calendar from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.