Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing String to Date without using SimpleDateFormat?

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);
like image 463
Kishan_KP Avatar asked Oct 21 '22 13:10

Kishan_KP


1 Answers

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];
like image 138
Pragnani Avatar answered Oct 27 '22 20:10

Pragnani