Convert number to date in java

I communicate with a server and I get this type of JSON response:

WeekEndingDate=/Date(1428638400000-0400)/

The function behind getting this number from a date is :

function ParseJSONDate(value) {
   return JSON.parseWithDate(JSON.stringify(value)).format("mm/dd/yyyy");
}

My question is: how do I get back the date, in the format mm/dd/yyyy from that value (/Date(1428638400000-0400)/) ?

Upvotes: 0

Views: 9878

Answers (3)

T.Gounelle
T.Gounelle

Reputation: 6033

You can use the SimpleDateFormat$parse() method in order to get a Date object, then reformat it with the wanted format. The parsing format is of the form:

  • ssssssssss (10 's'): date-time in seconds
  • SSS: milliseconds
  • Z: time zone, which is "-0400" in the example.

String input = "1428638400000-0400";
SimpleDateFormat inputFormat = new SimpleDateFormat("ssssssssssSSSZ");
Date myDate = inputFormat.parse(input);

SimpleDateFormat outputFormat = new SimpleDateFormat("MM/dd/yyyy");
String myDateAsString = outputFormat.format(myDate);
System.out.println(myDateAsString);

=> 04/10/2015

Upvotes: 2

mady
mady

Reputation: 119

Try this below code...

public static Date convertMillisecondsTodate(int givenMilliseconds) {
    String convertedMilliSeconds = Integer.toString(givenMilliseconds)
            .concat("000");

    /* DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); */

    long milliSeconds = Long.parseLong(convertedMilliSeconds);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(milliSeconds);
    Date date = new Date(milliSeconds);

    return date;

}

Upvotes: -2

vincent
vincent

Reputation: 1244

If i don't make a mistake the string "1428638400000-0400" represents two information :

  • 1428638400000 : the number of milliseconds
  • 0400 : the hour

So after splitting by '-'. You could create a date with :

Date d = new Date ("1428638400000");

After that you could print it with a DateFormat. Hope this help

Upvotes: 2

Related Questions