How to convert String to Date in java, whatever the system format

I am trying to convert a date string object to a date in java regardless of the format of the current system date. Because I want my custom date format to be stored in the database. Below is my code and please consult me.

public static String dateToString(String date){ if(date.equals("")) return ""; if(date == null || date.length() == 0){ return ""; } SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); try { Date l_date = format.parse(date); Calendar calendar = Calendar.getInstance(); calendar.setTime(l_date); String year = String.format("%04d", calendar.get(Calendar.YEAR)); String month = String.format("%02d", calendar.get(Calendar.MONTH)); String day = String.format("%02d", calendar.get(Calendar.DATE)); return year + month + day; } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); return ""; } } 

For SimpleDateFormat, it can only parse the format I heard.

 dateToString("16/04/2015"); 

It can convert for the above code. But, when I try in the following format

 dateToString("Thursday, April 16, 2015"); 

I am coming Nepalese date: error Thursday, April 16, 2015.

+7
java string
source share
3 answers

You are trying to convert the String format to EEEE, MMMM dd, yyyy in the format dd/MM/yyyy ...

Start by using the correct format for the String you are trying to convert, use the format you want to convert back ...

 SimpleDateFormat from = new SimpleDateFormat("EEEE, MMMM dd, yyyy"); SimpleDateFormat to = new SimpleDateFormat("dd/MM/yyyy"); String value = to.format(from.parse(dateString)); 

Now you can use something like DateUtils.parseDate(String, String[]) , which allows you to supply several different formats, but is still limited by what you know.

A better solution would be to store the Date value directly in the database.

+4
source share
  • You are passing the wrong SimpleDateFormater parameter
  • Using SimpleDateFormat format = new SimpleDateFormat ("EEEE, MMMM dd, yyyy") instead of SimpleDateFormat to = new SimpleDateFormat ("dd / MM / yyyy");
  • It solves your problem with an unconfirmed date.
0
source share

Try using something like this:

 public static String dateToString(String date){ if(date.equals("")) return ""; if(date == null || date.length() == 0){ return ""; } SimpleDateFormat formatIn = new SimpleDateFormat("dd/MM/yyyy"); SimpleDateFormat formatOut = new SimpleDateFormat("yyyy-dd-MM"); try { Date l_date = formatIn.parse(date); String result = formatOut.format(l_date); return result; } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); return ""; } } 

but if you want to put some data into the database, you can also use String java.sql.Date instead

something like that:

 SimpleDateFormat format = new SimpleDateFormat(); Connection conn = ... PreparedStatement ps = conn.prepareStatement("..."); long lDate = format.parse(sDate).getTime(); java.sql.Date dDate = new java.sql.Date(lDate); ps.setDate(1, dDate); 
0
source share

All Articles