views:

271

answers:

4

I have a requirement of sending the calendar object with time zone, like Tue Mar 03 13:43:00. I know that Calendar object always return with Tue Mar 03 13:43:00 CST 2009, so how can I create it without the time zone?

+1  A: 

You can use the DateFormat class (more specifically the SimpleDateFormat concrete subclass) to format the date however you want. Here is an example:

Calendar cal = Calendar.getInstance();
DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss");

String dateString = format.format(cal.getTime()));

Prints:

Fri Mar 06 15:50:26
Kip
+1 Never use Calendar.toString(); always use a formatter.
Jason Cohen
Thanks for the reply but the services which I am calling is looking for Calendar object so I can't use DateFormat ...
Calendar.getTime() returns a Date object.. see the code I just posted.
Kip
thanks Kip for sharing the code but it still not going to solve my problem since I need to pass a Calendar object to the service with time zone
A: 

You can use a simple Date object using;

Date d = new Date();
Date d2 = calendar.getTime();

Or the even simpler, time in milli-seconds.

long time = System.currentTimeMillis();

To avoid confusion over time zones, I suggest storing and setting all date/times as a standard time zone, such as GMT+0 and then converting the time for display etc as required.

Peter Lawrey
A: 

Upon rereading your question and your comments on my other answer, it sounds like the problem is that you want the result to be a Calendar object, not a String. So it isn't a question of formatting the String representation of the Calendar.

In this case, what you are asking is technically impossible: all implementations of the Calendar class exist to represent an internal time, which is stored as the number of milliseconds since the Unix epoch (Jan 1 1970 00:00 GMT). The time "5:00 am" in Boston is different from "5:00 am" in Seattle--they have different internal timestamps.

Your best bet would be to use the same time zone everywhere, and probably GMT makes the most sense for that purpose:

cal.setTimeZone(TimeZone.getTimeZone("GMT+0000"));
Kip
+1  A: 

It's not possible to have a Calendar object without a TimeZone. Really, that doesn't make any sense - Calendar seeks to represent "a specific instant in time" - how can you represent an instance in time without knowing what TimeZone the time you are representing is in?

So, there's a few things to consider:

  1. You say you need to pass this Calendar to some other component - does that API care what TimeZone the Calendars are in? Will that API ignore the TimeZone setting, use it to calculate a time difference against it's own local timezone, etc.?
  2. Are you aware that you can easily change the TimeZone of a Calendar instance?

    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getDefault()); // returns default TimeZone for your system
    cal.setTimeZone(TimeZone.getTimeZone("EST")); // Eastern Standard Time
    cal.setTimeZone(TimeZone.getTimeZone("Asia/Kolkota"); // UTC+5:30
    

Javadocs on Calendar, TimeZone.

matt b
btw looks like Calendar.setTimeZone(null) would cause some errors; which seems to support the notion that a Calendar is very closely tied to and needs it's TimeZone reference.
matt b