I am using Facebook graph API to retrieve user wall information. In that, I am getting the post created time value as:
created_time: "2012-04-19T09:00:02+0000"
Can anyone suggest me how to convert this time to UTC or epoch value in Java?
The format of date you receive is ISO 8601.
As described in Converting ISO8601-compliant String to java.util.Date (using Joda-Time):
DateTimeFormatter parser = ISODateTimeFormat.dateTimeNoMillis();
String jtdate = "2012-04-19T09:00:02+0000";
System.out.println(parser.parseDateTime(jtdate));
You basically need to parse the ISO8601 date format. There are libraries out there that would do it for you or you can create your own parser (relatively simply), e.g.
http://www.java2s.com/Code/Java/Data-Type/ISO8601dateparsingutility.htm
Related
I'm getting the following error when trying to pass a date object from AngularJS to java spring backend:
Failed to convert from type [java.lang.String] to type
[#javax.persistence.Column java.sql.Timestamp] for value
'2018-06-12T22:00:00.000Z'; nested exception is
java.lang.IllegalArgumentException: Timestamp format must be
yyyy-mm-dd hh:mm:ss[.fffffffff]
So far I tried to format the date object to a string in the expected format:
$filter('date')(date, "yyyy-MM-dd hh:mm:ss");
which leads to an error telling:
Error: [ngModel:datefmt] Expected 2018-06-13 12:00:00 to be a date
Seems like I need to pass a date object but I can't find a way to influence the date format AngularJS is attempting to convert to.
java.time.Instant
Your backend service is outdated, using a legacy class java.sql.Timestamp. That class was supplanted years ago by java.time.Instant.
If you make that change your backend to use Instant, you’ll have no problem passing a String such as 2018-06-12T22:00:00.000Z. That string is using a standard format defined in ISO 8601. That format is the ideal way to exchange date-time values as text.
The java.time classes use ISO 8601 formats by default. So no need to specify a formatting pattern.
I know Hibernate has been updated to support the java.time classes. I don’t know about JPA. (I don’t use either.)
You can convert a date string to a date object using any format like this:
Date parseDateString(String dateString){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = dateFormat.parse(dateString);
}
You can read more about it in the documentation
This question already has answers here:
Parsing ISO 8601 date format like 2015-06-27T13:16:37.363Z in Java [duplicate]
(2 answers)
Closed 4 years ago.
I need to transform a Twitter timestampe into a Java Date object,
here is an example of a value of a Timestampe: "2015-01-06T21:07:00Z"
Can you please give me sample of java code (standard Java) doing the job?
Thank you
I recommend you take advantage of the new Date/Time API introduced in Java 8, specifically Instant as follows:
Instant.parse("2015-01-06T21:07:00Z");
You can then perform a multitude of operations, but keep in mind that the object is immutable, so any changes to the instance (that aren't chained) must be stored in a separate variable.
Actually it is ISO 8601 format for UTC time zone.
It conforms with XML DateTime format as well.
So, to get actual java.util.Calendar or java.util.Date out of it you simply can use available in JDK
Calendar twitterCalendar = javax.xml.bind.DatatypeConverter.parseDateTime("2015-01-06T21:07:00Z");
Date twitterDate = javax.xml.bind.DatatypeConverter.parseDateTime("2015-01-06T21:07:00Z").getTime();
Just be aware: java.util.Date has no Time Zone information in it. Your string is in UTC, so if you try to print value of twitterDate you will see Date/Time in TimeZone of your computer/server. Still actual value of twitterDate stays the same
millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).
I am consuming an API that produces dates as Epoch timestamps but in string format:
{ "date":"1499762012700"}
Is there any way of getting this to go into a pojo as a Date object without writing some sort of custom serializer etc?
This works fine if the timestamp is a number but unfortunately this is the way it is given.
Is it a 9 digit timestamp? It would be helpful to have an actual example. If its unix time and do you want a java Date or a JODA object. I'd go with JODA
If its a unix 10 digit time stamp look here
Java: Date from unix timestamp
or
I need to pass datetimes between an android client, a python server and a mysql server.
It should work the following:
the android client sends the exact time from the client to the cherrypy python server (I guess the datetime object should be sent as a string?)
the server has to parse this string into something useful to work with as a datetime object
Now there a two cases:
the datetime object should be written into a mysql database (there is an attribute of type DATETIME in the related database table)
the server should retrieve a datetime from the mysql database and compare it with the parsed datetime object
After some background processes finished their work with the python datetime objects as input parameters, a new datetime object should be passed back to the android client
Does anyone know some good solution for solving these problems?
The best format for transferring datetime values is the ISO 8601 standard; they come in the format YYYY-mm-ddTHH:MM:SS+tz:tz, where the T is optional.
Python's datetime.datetime class can parse these with a simple extra module (see How to parse an ISO 8601-formatted date?). Output is just as easy with the .isoformat() method.
MySQL's DATETIME column type only deals with UTC values, but by default accepts ISO 8601 datetime strings (with a space instead of a T), so you'd have to cast your datetime objects to UTC (example with iso8601 module mentioned above):
import iso8601
utciso8601 = dt.astimezone(iso8601.iso8601.UTC).isoformat(' ')[:19]
I'd insert the timezone offset into the database too; simply use the tzname() method to retrieve it from the datetime object, then parse it out again when loading from MySQL with the iso8601.iso8601.parse_timezone() function.
# insertion:
cursor.execute('INSERT INTO dates VALUES(?, ?)', utciso8601, dt.tzname())
# querying
for row in query:
timezone = iso8601.iso8601.parse_timezone(row[1])
utcdt = iso8601.parse_date(row[0])
dt = utcdt.astimezone(timezone)
I don't know how well Android deals with dates and times, but surely it can handle ISO 8601 formats just fine.
How can I convert time in unix timestamp to normal time?
Your question is vague and ambiguous. I'll leave the timezone ambiguity away.
How can I convert time in unix timestamp to normal time?
I suspect that you're somehow obtaining a long or maybe a String value from the DB instead of a Date. In JDBC, you would normally like to use the appropriate methods to obtain the DB-specific datatypes. The MySQL TIMESTAMP datatype can be obtained by ResultSet#getTimestamp() which gives you a java.sql.Timestamp which in turn is a subclass of java.util.Date.
In a nut, the following should do:
Date date = resultSet.getTimestamp("columnname");
To format it further in a human readable format whenever you're going to present it to the enduser, use SimpleDateFormat. Click the link, it contains an overview of all patterns. Here's an example:
String dateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
To do all the other way round, use respectively SimpleDateFormat#parse() and PreparedStatement#setTimestamp().
Unix timestamp is seconds since "epoch". Java's currentTimeMillis are milliseconds since "epoch". You can get a Java Date object with a simple multiplication like this:
Date dateFromUnixTime = new Date( 1000l * unixTime) ;
From there, you can format it using the normal date formatting tools in Java.