2

I have parsed an object, containing a GregorianCalendar Object, from java, to JSON, and onwards into a Javascript Object. The object I have in Javascript looks like this when printed in console.log()

Object
dayOfMonth: 27
hourOfDay: 0
minute: 0
month: 4
second: 0
year: 2014

Is there a way to format this through something similar to Java's SimpleDateFormat, into a pattern like dd/mm-yy? Or is it a better approach to parse the Calendar-object to a string before turning it into JSON-format in the first place?

jumps4fun
  • 3,994
  • 10
  • 50
  • 96

1 Answers1

0

I'd take your second approach (GregorianCalendar.getTime() to your preferred String format) then simply new Date(dateString); within JavaScript once you need the date object. This makes for a smaller transmission. It appears either way will work for you.. just so you aren't losing data.

Stan Smith
  • 896
  • 7
  • 19
  • If there are no easy, standard ways of parsing the Javascript Object, I would agree. i created another class, similar to my existing JavaBean-class, named "mySimpleJavaBean", and in my existing parser-class, i made a static method to translate "myJavaBean" to "mySimpleJavaBean". I do not know if Calendar objects inside other objects even classify as "real" JavaBeans. This approach seems to be a predictable, stable and easy-to-read solution. – jumps4fun Mar 24 '14 at 14:34
  • Sounds good. Will your application require a TimeZone offset in the future... or are you using UTC or a single zone? The top answer in the following thread is an interesting read on date string formatting: http://stackoverflow.com/questions/10286204/the-right-json-date-format – Stan Smith Mar 24 '14 at 15:54