0

i try to get the current time like that :

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
String sendingDateAndTime = dateFormat.format(cal.getTime()).trim();

but i get the GMT time when i want the system time (and not the local time because my software will be executed in several countries so i can use the TimeZone object).

I need to use the date library and the GregorianCalendar library but i get the same wrong result.

Many people have the same problem but all the solution that i saw it's to put hard code like "Europe" or something else in the timezone object.

If someone can help you.

Thankssss

---------------------------- UPDATE ------------------------------------

I tried to use the System.currentTimeMillis() and to give it as parameter to calendar object, but i get the GMT time too

  • possible duplicate of [How to handle calendar TimeZones using Java?](http://stackoverflow.com/questions/230126/how-to-handle-calendar-timezones-using-java) – Gilbert Le Blanc Aug 28 '13 at 13:28

1 Answers1

0

How about this for the GMT time?

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

And this for the local computer time?

Calendar calendar = Calendar.getInstance();

DateFormat will default to the local time zone. The internal format of a Calendar object is the number of milliseconds past midnight, January 1, 1970, GMT.

Edited to add: When I run this code, I get my local time.

public static void main (String[] args) {
    Calendar calendar = Calendar.getInstance();

    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss z");
    String sendingDateAndTime = dateFormat.format(calendar.getTime());
    System.out.println(sendingDateAndTime);
}

When I run this code, I get GMT, although the time zone is still my local time zone.

public static void main (String[] args) {
    Calendar calendar = Calendar.getInstance();
    TimeZone timeZone = calendar.getTimeZone();
    int offsetFromUTC = timeZone.getOffset(calendar.getTimeInMillis());
    calendar.add(Calendar.MILLISECOND, -offsetFromUTC);

    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss z");
    String sendingDateAndTime = dateFormat.format(calendar.getTime());
    System.out.println(sendingDateAndTime);
}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • Gilbert : Calendar.getInstance() return the GMT time and not the system time. getTimeZone("GMT") same result. Example: now the local time is 15:59 and i get 12:59 (=GMT time) – Yoni Touitou Aug 28 '13 at 13:00
  • Very strange ! Someone said me that this problem is since windows 7, witch OS you have? Are you check if your local time is not the GMT time ? – Yoni Touitou Aug 28 '13 at 13:40
  • I'm on Windows XP, Java 1.6, and my local time is GMT-4 (United States, Eastern Daylight Time). – Gilbert Le Blanc Aug 28 '13 at 13:43
  • I ran my code on eclipse and it's work i get the good time. Apparently it's because the old version of Tomcat (4.1) – Yoni Touitou Aug 28 '13 at 13:51