1

I want to convert date time object in java to json string in format as below:

{"date":"/Date(18000000+0000)/"}

I did like this but it didn't give me the format i desired:

JSONObject object = new JSONObject();
object.put("date",new Date());

and the result of object.toString()

{"date":"Fri May 04 11:22:32 GMT+07:00 2012"}

i want the string "Fri May 04 11:22:32 GMT+07:00 2012" transform to "/Date(18000000+0000)/" (18000000+0000 here is just a example).

Thanks for your help.

MichaelP
  • 2,761
  • 5
  • 31
  • 36

8 Answers8

3

Here is my solution, although it is not a good way, but I finally find a workable solution.

SimpleDateFormat format = new SimpleDateFormat("Z");
Date date = new Date();
JSONObject object = new JSONObject();
object.put("date", "/Date(" + String.valueOf(date.getTime()) + format.format(date) + ")/");
Waqas Raja
  • 10,802
  • 4
  • 33
  • 38
MichaelP
  • 2,761
  • 5
  • 31
  • 36
1
 public static String convertToJsonDateTime(String javaDate)

  {
     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
        Date currentDate = null;
        try {
            currentDate = dateFormat.parse(javaDate);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        long time =currentDate.getTime();
    return  "\\/Date("+time+"+0000)\\/";
}
1

My solution : I think this is the simplest Way

DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
yourJsonObject.accumulate("yourDateVarible",dateFormat.format(new Date()));
SwapnilKumbhar
  • 347
  • 2
  • 5
  • 17
0

From json.org's description of JSONObject:

The values can be any of these types: Boolean, JSONArray, JSONObject, Number, and String, or the JSONObject.NULL object.

This library doesn't support the "complex" mapping of a Date to a JSON representation. You'll have to do it yourself. You may find something like SimpleDateFormat helpful.

Greg Kopff
  • 15,945
  • 12
  • 55
  • 78
0

The date format that you want is /Date(<epoch time><Time Zone>)/.
You can get the epoch time in java using long epoch = System.currentTimeMillis()/1000;(found at this link) and the time zone you can get by using the date and time patteren as Z. Then combine all the strings into one and store it to the Json Object.
Other possibility is that the time you are getting from iOS device may be of the pattern yyMMddHHmmssZ as got from here. Check the output on the iOS device at different times and identify the correct pattern.

Antrromet
  • 15,294
  • 10
  • 60
  • 75
0

If your format like this -> "DDmmYYYY+HHMM"

  1. DD -> Day (2 Digit)
  2. mm -> Month (2 Digit)
  3. YYYY -> Year (4 Digit)
  4. HH -> Hour
  5. MM -> Minute

Than you can do like this:

SimpleDateFormat format = new SimpleDateFormat("DDmmYYYY+HHMM");
JSONObject object = new JSONObject();
object.put("date", "/Date(" + format.format(new Date()) + ")/");
Crazenezz
  • 3,416
  • 6
  • 31
  • 59
  • that's great, but i not sure "18000000+0000" equivalent to format "DDmmYYYY+HHMM". ok i will research more about this format. – MichaelP May 04 '12 at 07:01
0

I suppose that's a representation of ISO international date-time format.

YYYYMMDD+HHMM

I think now you will be able to create that string

may be like,

Date d=new Date(); String tmp=""; tmp="/Date("d.getYear()+""+d.getMonth()+""+d.getDate()+"+"+d.getHours()+""+d.getMinutes()+")/";

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
0

Upgrade one of the previous answer:

 public static String convertToJsonDateTime(Date dateToConvert) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
        long time = dateToConvert.getTime();
        return "/Date(" + time + "+0000)/";
    }