0

I am trying to convert Json date string to java date format. However, it gives error when it comes to "return df.parse( tarih )" line.

JSON :

{"DateFrom":"\/Date(1323087840000+0200)\/"}

Java code :

private Date JSONTarihConvert(String tarih) throws ParseException
{


    SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssz" );


    if ( tarih.endsWith( "Z" ) ) {
        tarih = tarih.substring( 0, tarih.length() - 1) + "GMT-00:00";
    } else {
        int inset = 6;


        String s0 = tarih.substring( 6, tarih.length()-1 - inset );
        String s1 = tarih.substring( tarih.length()- inset,tarih.length()-2 );

        tarih = s0 + "GMT" + s1;
    }


        return df.parse( tarih );

}

When I call this method, tarih parameter is: /Date(1323087840000+0200)/

essbek
  • 59
  • 1
  • 3
  • 10
  • 2
    What exactly is the error and could you also post an example of tarih? :) – nuala Aug 16 '12 at 09:24
  • The date provided hasn't any `-`. Then you are transforming that string in some other thing without `-`, too. Then your date format cannot parse it correctly because it expects that `-`. – helios Aug 16 '12 at 09:26
  • Tarih is Date from Turkish, basically tarih is JSON date he is trying to parse. – d1e Aug 16 '12 at 09:28

6 Answers6

5

As you're interested in a Date object and the JSON occurs to me to be a unix timestamp.
Therefore I'd recommend you the Date(long milliseconds) constructor :)

private Date JSONTarihConvert(String tarih) throws ParseException{
    long timestamp = getTimeStampFromTarih(tarih);
    return new Date(timestamp);
}

Where getTimeStampFromTarih extracts the milliseconds before the occurrence of "+"

nuala
  • 2,681
  • 4
  • 30
  • 50
3

This will work surely

String date = "/Date(1376841597000)/";
Calendar calendar = Calendar.getInstance();
String datereip = date.replace("/Date(", "").replace(")/", "");
Long timeInMillis = Long.valueOf(datereip);
calendar.setTimeInMillis(timeInMillis);
System.out.println(calendar.getTime().toString("dd-MMM-yyyy h:mm tt"));//It Will Be in format 29-OCT-2014 2:26 PM
Rameshwar Trivedi
  • 414
  • 1
  • 4
  • 12
3

First replace string like this :

String str= ConvertMilliSecondsToFormattedDate(strDate.replace("/Date(","").replace(")/", ""));

Then Convert it like this:

public static String ConvertMilliSecondsToFormattedDate(String milliSeconds){
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(Long.parseLong(milliSeconds));
    return simpleDateFormat.format(calendar.getTime());
}
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
2

You will Need to type cast date:

String rawdate = "/Date(1995769286000)/";
Calendar calendarins = Calendar.getInstance();
String datereip = rawdate.replace("/Date(", "").replace(")/", "");
Long timeInMillis = Long.valueOf(datereip);
calendar.setTimeInMillis(timeInMillis);
System.out.println(calendar.getTime().toString("dd-MMM-yyyy h:mm tt"));
1

Unless you have a reason not to, you should be using a parser to serialize and de-serialize objects. Like Jackson parser.

Chris
  • 5,584
  • 9
  • 40
  • 58
0

java.time

This is the modern answer (since 2014).

First I want to make sure the timestamp I have really lives up to the format I expect. I want to make sure if one day it doesn’t, I don’t just pretend and the user will get incorrect results without knowing they are incorrect. So for parsing the timestamp string, since I didn’t find a date-time format that would accept milliseconds since the epoch, I used a regular expression:

    String time = "/Date(1479974400000-0800)/";
    Pattern pat = Pattern.compile("/Date\\((\\d+)([+-]\\d{4})\\)/");
    Matcher m = pat.matcher(time);
    if (m.matches()) {
        Instant i = Instant.ofEpochMilli(Long.parseLong(m.group(1)));
        System.out.println(i);
    }

This prints:

2016-11-24T08:00:00Z

It is not clear to me whether you need to use the zone offset and for what purpose. But since we’ve got it, why not retrieve it from the matcher and use it for forming an OffsetDateTime, a date and time with UTC offset. Here’s how:

        ZoneOffset zo = ZoneOffset.of(m.group(2));
        OffsetDateTime odt = OffsetDateTime.ofInstant(i, zo);
        System.out.println(odt);

2011-12-05T14:24+02:00

If you need an old-fashioned java.util.Date for some legacy API:

        System.out.println(Date.from(i));

Or if using the backport mentioned in the links below:

        System.out.println(DateTimeUtils.toDate(i));

On my computer it prints

Mon Dec 05 13:24:00 CET 2011

The exact output will depend on your time zone.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161