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");
0

2 Answers 2

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());
Sign up to request clarification or add additional context in comments.

2 Comments

Impressive how you managed to find the solution to your own question in less than a minute! :)
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
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.

1 Comment

I think you get me wrong! I want to get to the same result in java as php does using this command gmdate("n/j/Y g:i:s A")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.