JSP and JavaScript conversion date

I have a problem trying to convert a specific type of date.

My goal is to load it into this format: dd/MM/yyyy

Current Date Format: Thu Apr 04 00:00:00 EEST 2013

When I warn about using JavaScript, it replies that it is not a date. I used a lot of solutions, such as formatting in JSP:

 <fmt:formatDate value="${theDate}" pattern="dd/MM/yyyy"/> 

Result Error:

 Attempt to convert String "Thu Apr 04 00:00:00 EEST 2013" to type "java.util.Date", but there is no PropertyEditor for that type. 

And even in Javascript:

 var dateCreation = new Date(theDate); 

The problem with JavaScript is that it says dateCreation not a date. Any ideas?

+4
source share
4 answers

Problem with your date string "Thu Apr 04 00:00:00 EEST 2013"

when i tried with your date string to get IllegelArgumentException

change "Thu Apr 04 00:00:00 EEST 2013" to "Thu Apr 04 00:00:00 EST 2013" , then work fine.

I can not guess why you received this EEST (one E extra).

  public static void main(String[] args) { Date date = new Date("Thu Apr 04 00:00:00 EST 2013"); System.out.println(date); } 

You get an error in javascript because you are passing the same JS string as me.

+1
source

fmt: formatDate expects a java date object as a value. In your case, it looks like the Date object is not a date object, but a string representation of a date object. Specify a date object and this should work.

Also in JavaScript, what is the value of the param parameter and where is it defined?

0
source

The date object in JavaScript has the following constructors:

 var d = new Date(); var d = new Date(milliseconds); // from epoch var d = new Date(dateString); // "yyyy-mm-dd hh:mm:ss" var d = new Date(year, month, day, hours, minutes, seconds, milliseconds); 

Make sure you use one of them. Keep in mind that the month for the last constructor mentioned is 0.

0
source

EST is GMT-05: 00, and EEST is GMT + 03: 00. The difference arises, for example. when parsing "Thu Apr 04 23:00:00 EEST 2013". Java (7) can parse EEST, but cannot format it in EEST.

  String s = "Thu Apr 04 23:00:00 EEST 2013"; SimpleDateFormat sdfParse = new SimpleDateFormat("E MMM d H:m:sz yyyy", Locale.ENGLISH); Date d = sdfParse.parse(s); SimpleTimeZone stz = new SimpleTimeZone(3 * 3600000 /* GMT+03:00 */, "EEST"); GregorianCalendar cal = new GregorianCalendar(stz, Locale.ENGLISH); cal.setTime(d); String ddmmyyyy = String.format("%02d/%02d/%4d", cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.YEAR)); System.out.println(s+"\n"+ddmmyyyy); 

In JavaScript:

 function format2(s) {s="0"+s;return s.substr(s.length-2);} function dateFormat(theDate) { //theDate = "Thu Apr 04 00:00:00 EEST 2013"; var timestamp = Date.parse(theDate.replace(/EEST/, "GMT+0300")); var datePseudoGMT = new Date(timestamp+3 * 3600000); var dateCreation = format2(datePseudoGMT.getUTCDate()) +"/"+format2(datePseudoGMT.getUTCMonth()+1) +"/"+format2(datePseudoGMT.getUTCFullYear()); console.log(dateCreation); return dateCreation; } 
0
source

All Articles