1

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");
Mohsen Mirhoseini
  • 8,454
  • 5
  • 34
  • 59

2 Answers2

1

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());
Mohsen Mirhoseini
  • 8,454
  • 5
  • 34
  • 59
  • Impressive how you managed to find the solution to your own question in less than a minute! :) – walen Oct 10 '16 at 09:13
  • 1
    The point of this question was sharing new knowledge that I achieved. I have just solved my problem yesterday. By the way, thanks for your answer @walen – Mohsen Mirhoseini Oct 10 '16 at 09:25
0

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.

walen
  • 7,103
  • 2
  • 37
  • 58