Reputation: 8882
I am working on a Streaming Android application which I have to convert some php codes to java. How can I convert this date format from php to java?
$today = gmdate("n/j/Y g:i:s A");
Upvotes: 1
Views: 2895
Reputation: 8882
This date format in php is interpreted like this:
n: Numeric representation of a month, without leading zeros
j: Day of the month without leading zeros
Y: A full numeric representation of a year, 4 digits
g: 12-hour format of an hour without leading zeros
i: Minutes with leading zeros
s: Seconds, with leading zeros
A: Uppercase Ante meridiem and Post meridiem - AM/PM
and the same date format in java is like this:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("M/d/yyyy h:mm:ss a");
String today = simpleDateFormat.format(new Date());
Upvotes: 1
Reputation: 7273
To get a new java.util.Date
object from your PHP date string, in Java:
String phpDateString = "7/24/2016 12:21:44 am";
SimpleDateFormat sdf = new SimpleDateFormat("M/d/yyyy h:mm:ss a");
Date javaDate = sdf.parse(phpDateString);
System.out.println(javaDate);
System.out.println(sdf.format(javaDate));
Output:
Sun Jul 24 00:21:44 CEST 2016
7/24/2016 12:21:44 AM
OP's self-answer was very informative, but it had an error in the Java expression (it's lowercase h
for am/pm hours) and didn't include code to actually parse the PHP string into a Java Date
object, which was the original question.
Upvotes: 0