5

I am developing a spring application and in one of my controller i have following lines to parse from string to date and format the parsed date to required format. But again i need to parse back formatted string into date without using any SimpleDateFormat, so is it possible to do so ?

SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");

Date pick=dateFormat.parse(request.getParameter("pickDate"));
String pick_date=dateFormat2.format(pick);
arahant
  • 2,203
  • 7
  • 38
  • 62
Kishan_KP
  • 4,488
  • 6
  • 27
  • 46

3 Answers3

5

Edit:

I found in the wikipedia that china has the locale yyyy-MM-dd. check this reference date format by country set locale to China you'll get the required date format

Try this

String d1="12-27-2010";
    Stirng[] splitdata=d1.split("-");

   int month=Integer.parseInt(splitdata[0]);
 int day=Integer.parseInt(splitdata[1]);
int year=Integer.parseInt(splitdata[2]);

Calender cal=Calender.getInstance(Locale.CHINA);
cal.set(year,month,day);
Date d=cal.getTime();

This should work


If you know your data format you can do that. by using simple string operations

Ex: if your data format is

MM-dd-yyyy

then you can convert to yyyy-MM-dd like this

String d1="12-27-2010";
Stirng[] splitdata=d1.split("-");

String s2= splitdate[2]+"-"+splitdate[0]+"-"+splitdate[1];
Pragnani
  • 20,075
  • 6
  • 49
  • 74
0

You can use Concatenation Operator(+) for that

Abdul Manaf
  • 13
  • 1
  • 4
-2

Yes possible; just write the code to do the parsing. Should not be that difficult ...

Amit
  • 1,836
  • 15
  • 24
  • 2
    how to do it without using SimpleDateFormat? – Kishan_KP Mar 26 '13 at 04:50
  • 1
    just like how you program any other stuff. Pick up the string, break it up, 'read' the string, make a date out of it. I mean if you are NOT to use the inbuilt libraries of the language, you got to implement it yourself. Whoever wrote SimpleDateFormat did write the parsing code. You can write something similar. And a web search should yield you sample code as well. But it is most of the time a futile exercise .... difficult to get it right. What exactly is the problem with SimpleDateFormat ? – Amit Mar 26 '13 at 04:53