A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Win a copy of
video training course this week in the
Java in General
Timestamp Time Difference
Joined: Feb 19, 2004
Aug 10, 2007 03:24:00
My program runs on one remote unix machine. And I use
String today = new Timestamp(System.currentTimeMillis()).toString();
to get the timestamp. But weird thing is it prints out a very strange value, different from the output using the date command on unix.
When issue a date command on bash, it prints: Fri Aug 10 01:51:21 GMT 2007
program prints: 2007-08-10 15:51:07.372
My local time is: 2007-08-10 16:51:00
Why java gave me such a strange value which is different from either the remote server time and my local time?
Joined: Jul 09, 2007
Aug 10, 2007 03:38:00
look at the toString mehtod. It returns the nanoseconds too.
Jesper de Jong
Joined: Aug 16, 2005
Aug 10, 2007 05:05:00
The date command prints the time in GMT.
Your Java program prints the time in your local timezone, but probably without taking summertime into account. The ".372" are the milliseconds.
Try this to see in which timezone Java prints the time and date:
Date now = new Date(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); System.out.println(df.format(now));
If you want the date and time printed in a specific timezone, you can set the timezone on the
object. For example:
Date now = new Date(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); df.setTimeZone(TimeZone.getTimeZone("GMT")); System.out.println(df.format(now));
Java Beginners FAQ
JavaRanch SCJP FAQ
The Java Tutorial
Java SE 8 API documentation
Joined: Feb 19, 2004
Aug 11, 2007 20:39:00
Thank you guys! I got it. Jesper is right. Java printed my local time and took summer time into consideration.
Actually my Java program was wrapped into another main program, and it took care of the time thing. That's why I had the confusion.
It is sorta covered in the
JavaRanch Style Guide
subject: Timestamp Time Difference
Help with System Time
Problem computing time based on TimeZones
EJB30-JBOSS sample tutorial doesn't works!!
time stamp question
oracle date format
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2015