I am trying to get the the day and time in UTC as milliseconds but repeatedly get the same problem. The result should be something like '63530139420000' but each time the value '1394547490884' is returned.
To get the date and time in UTC I use the following method:
long dateutc = System.currentTimeMillis();
Can anyone tell me what is the problem?
You can use this.
DateFormat df = DateFormat.getTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateutc = df.format(new Date());
Your code correctly gets the number of milliseconds since January 1, 1970 00:00:00 UTC.
Interestingly if you take the value your teacher has specified and divide by the number of milliseconds in a year you get 2014.5. So either your teacher doesn't know what currentTimeMillis() does or he wants milliseconds since year 0 (which doesn't make any sense to me).
To calculate number of milliseconds since a given date all you need to do is to create two date instances and subtract the milliseconds values from getTime().
Related
I have tried all the ways in all the other questions on SO, and I can't get it to work. It is making me want to kill myself.
I have a set of times which are something like "04:00 AM AEST", except the AEST is a glitch, they should be GMT. What I want to do is change them to "04:00 GMT", and then convert them up to the correct AEST times (which in this example would be "14:00 AEST"). I have tried everything, and nothing works. The closest was to manually make a new DateTime using each individual value from the original date, e.g.
DateTime dt = new DateTime(origdate.year, origdate.month, origdate.day, origdate.hour, origdate.minute, origdate.second, timezone.GMT)
But for some reason the results came out four and a half minutes over, which is weird because timezones differ on hours and half hours.
1st Method By following lines you will get GMT time in specified format :
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Date currentLocalTime = cal.getTime();
DateFormat date = new SimpleDateFormat("dd-MM-yyy HH:mm:ss z");
date.setTimeZone(TimeZone.getTimeZone("GMT"));
String gmtTime = date.format(currentLocalTime);
Hence, from GMT you can derive the time of any place.
2nd Method You can get system time of current place in milliseconds by:
Long current_time = System.currentTimeMillis() / 1000L;
Hope it helps.
I am getting current time from Ruby on Rails webservice in Unix Timestamp format (ie. in seconds from 1 Jan 1970), the timezone on server is UTC.
In Java I am trying to convert local current time to UTC time. But every time it is giving 6+ minutes ahead time. I want to get the difference of UTC current time and the time returned from service. My Java code is -
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
Date utc_current = new Date(System.currentTimeMillis());
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
long serverTime = 1424936268000L;
long resTime = sdf.getCalendar().getTimeInMillis() - serverTime;
System.out.println("Time Diff : " + resTime);
Where serverTime is the time I am getting from webservice. And the value for resTime shows negative value which is approx 6+ minutes.
So my question is why UTC timezone giving ahead time for System.currentTimeMillis?
In contrast to the assumption in a comment of of #JB Nizet the expressions sdf.getCalendar().getTimeInMillis() and System.currentTimeMillis() are not equivalent. Proof:
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
System.out.println("date via System.currentTimeMillis()=" + f.format(utc_current));
System.out.println("date via sdf.getCalendar()=" + f.format(new Date(resTime)));
Output:
date via System.currentTimeMillis()=2015-02-26T12:19:09
date via sdf.getCalendar()=1889-12-31T04:41:21
If you carefully study the source code of SimpleDateFormat and DateFormat you will find within the initialization part code like:
private void initializeDefaultCentury() {
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add( Calendar.YEAR, -80 );
parseAmbiguousDatesAsAfter(calendar.getTime());
}
The conclusion is to strictly avoid the method getCalendar() on your DateFormat-object. It is only used as intermediate mutable object for internal format and parse processing. It is hard to say what you will really get as time this way. Instead use directly System.currentTimeMillis() to compare your local time with server time.
Another problem is the pattern you use. "dd-MM-yyyy hh:mm:ss" is probably not correct because it uses the clock hour of half day in range 1-12 but the information for am/pm is missing. Use better the pattern symbol HH. Check the documentation of webservice for the right format.
Make sure the the clock on the server and on the client machine are synchronized. The 6 minutes could simply be an offset between the two.
I have a .db file that I retrieved from my iCloud account. I am trying to parse the date field from one of the tables. I'm doing this in Java. I'm not quite sure how it's stored because it seems like it's just a number.
I am taking taking this value and parsing it into a long which a pass to a Date object in java. However, it seems to always be sometime in January of 1970.
Value stored for date: 356898417 gives me 1970-01-04 when I call Date.toString
The default Date Constructor accepts one argument that equals the number of milliseconds that have elapsed since midnight, January 1, 1970.
Date(long millisec)
Provide the actual datetime stored in the iCloud to be more specific.
If that numeric value is given in seconds you need to multiply it by 1000 to make it milliseconds.
eg:
String dateAsText = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(new Date(356898417 * 1000L));
I figured it out. They are stored in Mac absolute time (seconds)
long valueStoredInIphone = (something);
long millis = (valueStoredInIPhone * 1000) + 978307200000L;
Date dateSent = new Date(millis);
http://linuxsleuthing.blogspot.com/2012/10/whos-texting-ios6-smsdb.html
In my program I need to read a Time value from the database and then convert that value to milliseconds.
I do it like this :
Time t = getTimeFromDatabase();
long millis = t.getTime();
The problem is that the value of millis corresponds to time value which is 1 hour earlier than what is entered in the database.
For example: let's say that the value in the DB is 09:30:00. So if I do this:
Time t = getTimeFromDatabase();
System.out.println(t.toString);
System.out.println(t.getTime);
Output would be:
09:30:00
30600000
Now... 09:30:00 is ok. That's how it is in the DB.
But 30600000 / 3600000 = 8.5 (3600000 is milliseconds per hour). Which means that this value in milliseconds corresponds to 08:30:00.
The correct value for 09:30:00 should be 34200000.
My question is how can I get correct value regardless of time zone (I am in UTC +1, so I guess that this has something to do with my problem).
I have tried with other time values but it is always the same (1 hour earlier).
Thanks in advance.
When you parse the data from DB into a Time object, you should use a formatter, and set the proper time zone.
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
timeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = timeFormat.parse(dataFromDB); // dataFromDB is a "09:30:10" like String
System.out.println(timeFormat.format(date)); // will print time in HH:mm:ss format
System.out.println(date.getTime()); // will print milliseconds
How can I get the current local wall clock time (in number of millis since 1 Jan 1970) in London? Since my application can run on a server in any location, I think I need to use a TimeZone of "Europe/London". I also need to take Daylight Savings into account i.e. the application should add an hour during the "summer".
I would prefer to use the standard java.util libraries.
Is this correct?
TimeZone tz = TimeZone.getTimeZone("Europe/London") ;
Calendar cal = Calendar.getInstance(tz);
return cal.getTime().getTime() + tz.getDSTSavings();
Thanks
I'm not sure what this quantity represents, since the "number of millis since 1 Jan 1970" doesn't vary based on location or daylight saving. But, perhaps this calculation is useful to you:
TimeZone london = TimeZone.getTimeZone("Europe/London");
long now = System.currentTimeMillis();
return now + london.getOffset(now);
Most applications are better served using either UTC time or local time; this is really neither. You can get the UTC time and time in a particular zone like this:
Instant now = Instant.now(); /* UTC time */
ZonedDateTime local = now.atZone(ZoneId.of("Europe/London"));
Others have said that it may well not be a good idea to do this - I believe it depends on your situation, but using UTC is certainly something to consider.
However, I think you've missed something here: the number of seconds which have occurred since January 1st 1970 UTC (which is how the Unix epoch is always defined - and is actually the same as in London, as the offset on that date was 0) is obtainable with any of these expressions:
System.currentTimeMillis()
new Date().getTime()
Calendar.getInstance().getTime().getTime()
If you think about it, the number of milliseconds since that particular instant doesn't change depending on which time zone you're in.
Oh, and the normal suggestion - for a much better date and time API, see Joda Time.
To get the current time in London:
SimpleDateFormat f = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
f.setTimeZone(TimeZone.getTimeZone("Europe/London"));
System.out.println(f.format(GregorianCalendar.getInstance().getTime()));