How to parse date from database in iPhone - java

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

Related

Why UTC timezone giving ahead time for System.currentTimeMillis in Java?

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.

java.sql.Time getTime does not return good value

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

Java: Compare Date.getTime with a different date [duplicate]

This question already has answers here:
How to convert date time from one time zone to another time zone
(5 answers)
Closed 7 years ago.
There is the date date1 given with the format YY-MM-dd hh:mm:ss.SSS
I want to compare
date1.getTime()
with one retrieved by doing
new Date().getTime()
There is
SimpleDateFormat sf = new SimpleDateFormat("YY-MM-dd hh:mm:ss.SSS");
Date date1 = sf.parse(date1AsString);
...
compare date1.getTime() with new Date().getTime();
How can I bring these two dates to a common 'timezone' to compare them?
How can I obtain date1 to be on the same 'time length' as new Date()? I want to have the same timezone...
Thanks
From the javadoc of Date
The class Date represents a specific instant in time, with millisecond precision.
An instance in time is agnostic of our definition of time enhanced with time zones. Right now is the same for you and me, regardless of the fact that we are (potentially) in different time zones.
What adds the notion of a time zone is the DateFormat
The date is represented as a Date object or as the milliseconds since January 1, 1970, 00:00:00 GMT.
When you invoke Date#getTime(), you get back
the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
This is something you can use to compare Date objects since they have the same root. Similarly, the compareTo will return
the value 0 if the argument Date is equal to this Date; a value
less than 0 if this Date is before the Date argument; and a value
greater than 0 if this Date is after the Date argument.
You cannot compare two dates if their timezone information is unknown.
For example if your date1AsString variable is simply 2014-12-16 16:00:00 then you cannot tell if it is greater than or less than 2014-12-16 20:00:00+0000.
It looks like first date is 4 hours less than second one; but if someone adds that the first date is Pacific Time (UTC-0800) then it would actually be 4 hours more than the second date (2014-12-16 16:00:00-0800 = 2014-12-17 00:00:00+0000).
So, if date1AsString has an unknown timezone then you cannot convert it to UTC or anything else for comparison.
getTime() Returns the number of milliseconds since January 1, 1970, 00:00 :00. You will want to compare the two longs, roughly like so:
long time = new Date().getTime();
long time2 = date1.getTime();
if(time>time2)
System.out.println(String.format("time is greater than time2 by %d", time-time2)
else
System.out.println(String.format("time2 is greater than time by %d", time2-time)
if you know Timezone
date1.setTime(date1.getTime()+/-(3600000*hours)) to convert, if date1 time is GMT to go to GMT+2 hours must be 2 and sing must be +
Typically any server environment is going to be using GMT so you wont have to worry about the timezone when comparing dates, only when formatting the value to be display to users.
You can compare two dates like so:
SimpleDateFormat sf = new SimpleDateFormat("YY-MM-dd hh:mm:ss.SSS");
Date date1 = sf.parse(date1AsString);
boolean isBefore = date1.before(new Date());
boolean isAfter = date1.after(new Date());
Alternatively, you can compare the numeric values of the unix time to get the same result:
...
boolean isBefore = date1.getTime() < new Date().getTime();
boolean isAfter = date1.getTime() > new Date().getTime();
The Date Class has methods like before(date) and after(date). You should take a look into the Date Class.
So I think what you are looking for is to set the time zone of the SimpleDateFormat:
sf.setTimeZone(TimeZone.getTimeZone("EST"));

Having problems on getting actual time in UTC Android

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().

How to define the date format?

I have the int number 2455449 and I know that represents the date 09/09/2010. How can I define the date format wihch is used? I need to generate a new date in this format. It will be used for http requests. I suppose that is Julian but I'm not sure. I tried to convert this number to the date but it didn't return the right date of 09/09/2010. Probably I used a wrong SimpleDateFormat("mm/dd/yy") or Calendar.XXXX (e.g.Calendar.DAY_OF_YEAR)
var now = new Date();
now.format("m/dd/yy");
// Returns, e.g., 6/09/07
// Can also be used as a standalone function
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
// Saturday, June 9th, 2007, 5:46:21 PM
// You can use one of several named masks
now.format("isoDateTime");
// 2007-06-09T17:46:21
// ...Or add your own
dateFormat.masks.hammerTime = 'HH:MM! "Can\'t touch this!"';
now.format("hammerTime");
// 17:46! Can't touch this!
http://blog.stevenlevithan.com/archives/date-time-format
looks like an equation with unknown function.f(d,m,y)=D; Where d the day, m month,y year and D is int date. And without loss of generality we can assume that this mapping should be one to one i.e. every valid (d,m,y) combination should map to a unique positive integer (>=0) and every positive integer must represent a valid and unique (d,m,y) tuple.So the most obvious choice of function f (based on the property of dates) is number of days elapsed since the first day, which satisfies our conditions. so now we have boundary condition.f(d1,m1,y1)=0;
f(9,9,2010)= 2455449;where d1,m1,y1 represents the reference date like epoch in unix timestamp. Using the obvious function (see above), (d1,m1,y1) comes out to be (10 5 -4713). So the DatFormat used is number of days elapsed since 10th June 4713 B.C. Approximately.
It is a Julian day number, and it counts the number of days since January 1, 4713 BC Greenwich noon in the Julian proleptic calendar.
To convert from a JD to a unix time stamp:
unix_time_stamp = ( JD -2440587.5) * 86400
To convert from unix time stamp to JD:
JD = (unix_time_stamp / 86400) + 2440587.5
Note that JD is counted from the noon, not midnight. That's why it's .5 at the end of the addition.
Update If you want to use it in javascript (that uses milliseconds since the epoch)
function dateFromJulianDay(julian_day) {
return new Date( (julian_day - 2440587.5) * 86400000);
}
function dateToJulianDay(date) {
// date should be a javascript Date object
// or a variable with milliseconds since the unix epoch 1 jan 1970
return ( date / 86400000) + 2440587.5;
}
console.log(dateFromJulianDay(2455449));
console.log(dateToJulianDay(new Date(2010,9-1,9)));
Remember that the month in the Date constructor is 0-11, whats why I do -1 above.

Categories

Resources