7

I'm trying to convert a date string to date object in java regardless of current system date format. Because I want to get my custom date format to store in database. The following is my code and please advice me the way.

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 coded.

dateToString("16/04/2015");

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

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

I go Unparseable date: "Thursday, April 16, 2015" error.

Vaibhav Mule
  • 5,016
  • 4
  • 35
  • 52
Thiha Zaw
  • 806
  • 1
  • 9
  • 25
  • Use `return new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());` as month in calendar is counted from 0. – Joop Eggen Apr 16 '15 at 07:32
  • Do not rely upon strings for communicating with your database. For date-time values, use date-time objects. As of JDBC 4.2 and later, use the java.time classes with `PreparedStatement::setObject` and `ResultSet::getObject`. – Basil Bourque May 06 '17 at 00:45

3 Answers3

4

You're trying to convert a String in EEEE, MMMM dd, yyyy format with the format of dd/MM/yyyy...

Start by using the correct format for the String you trying to convert, the use what ever format you want to convert it 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 could use something like DateUtils.parseDate(String, String[]) which allows to supply a number of different formats, but is still limited to what you might know.

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you @MadProgrammer. But, I don't want to store Date value directly in the database because client computer may have different date format and I just want to convert them back to my custom format before inserting to database. – Thiha Zaw Apr 16 '15 at 07:24
  • That's exactly my point though, store the `Date` in the database, then you don't have to care about the format (and you shouldn't), you should, where ever possible provide formatting of the objects in a format which the user is expecting, not what you want – MadProgrammer Apr 16 '15 at 07:27
0
  • You are passing wrong parameter to SimpleDateFormater
  • Use SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM dd, yyyy") instead of SimpleDateFormat to = new SimpleDateFormat("dd/MM/yyyy");
  • It resolve your Unparseable date issue.
Ranjeet
  • 636
  • 6
  • 12
  • Thank you @Ranjeet. But, I mean is I just want to convert any date format to my custom format. Is there any way to convert variable date format to a specific format? – Thiha Zaw Apr 16 '15 at 07:29
  • @Thiha Zaw : Then you need to write regex for all possible combination. – Ranjeet Apr 16 '15 at 07:33
0

Try to use 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 database you may also use instead String java.sql.Date

something like this:

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);
THM
  • 579
  • 6
  • 25