3

I am trying to convert date from one format to another, the date entered is in this format : 'mm-dd-yyyy' to 'yyyy-mm-dd'.

I received the date from webpage in 'mm-dd-yyyy' format and when I insert this date in MySQL using Hibernate, the date changes to some anonymous value.

import java.io.*;
import java.text.*;
import java.util.*;

class test{
  public static void main(String... s) throws Exception{
    Date date ;
    String datestr;
    DateFormat dateFormat1 = new SimpleDateFormat("mm-dd-yyyy");
    DateFormat dateFormat2 = new SimpleDateFormat("yyyy-mm-dd");

    date = dateFormat1.parse("01-01-2015");
    datestr = dateFormat1.format(date);

    System.out.println(date);
    System.out.println(datestr);

    date = dateFormat2.parse(datestr);
    datestr = dateFormat2.format(date);

    System.out.println(date);
    System.out.println(datestr);
  }
}

Expected output:

Thu Jan 01 00:00:00 IST 2015
2015-01-01

Actual output:

Thu Jan 01 00:01:00 IST 2015
01-01-2015
Thu Jul 08 00:01:00 IST 6
0006-01-08

The time of day is 00:01 instead of 00:00, and the latter date is a mystery.

1
  • I recommend that you use LocalDate from java.time, the modern Java date and time API, for a date and DateTimeFormatter for parsing it. java.util.Date had design problems, DateFormat and SimpleDateFormat were notorious trouble makers, and all had already been supplanted by java.time 10 years ago, before this question was asked. Commented Apr 26, 2024 at 11:30

5 Answers 5

2

You can try like this;

        DateFormat originalFormat = new SimpleDateFormat("MM-dd-yyyy", Locale.ENGLISH);
        DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = originalFormat.parse("01-21-2013");
        String formattedDate = targetFormat.format(date); 
        System.out.println(formattedDate);

For Date type result;

    DateFormat originalFormat = new SimpleDateFormat("mm-dd-yyyy", Locale.ENGLISH);
    DateFormat targetFormat = new SimpleDateFormat("yyyy-mm-dd");
    Date date = originalFormat.parse("01-21-2013");
    String formattedDate = targetFormat.format(date); 
    java.util.Date dtt = targetFormat.parse(formattedDate);
    java.sql.Date ds = new java.sql.Date(dtt.getTime());
    System.out.println(ds);
    System.out.println(dtt);
    System.out.println(formattedDate);

End the output is;

2013-01-21
Mon Jan 21 00:01:00 EET 2013
2013-01-21
Sign up to request clarification or add additional context in comments.

5 Comments

thanks a lott but again here I will get the date in string , I want that string in Date type
Could you try last one, I 'm handling with type java.sql.Date
it worked , somewhat but when I used this input for Document Date : 2013-11-30 what the date got converted to : 2013-01-30
Oh sorry, you must use MM instead of mm, I editted the answer.
yes it worked, I changed it MM and it worked, thanks a lotttt :D
1

We can do by using java 8.

DateTimeFormatter fromFormatter = DateTimeFormatter.ofPattern("MM-dd-yyyy");
DateTimeFormatter toFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.parse("01-21-2013",fromFormatter);
String requiredDate = localDate.format(toFormatter);
System.out.println(requiredDate);

This outputs:

2013-01-21

Still simpler and easier, save the formatting step and pass the LocalDate object to your MySQL database. See Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2.

We can do with ZoneId also, while parsing.

ZoneId id = ZoneId.of("GMT");
ZonedDateTime zonedDateTime = ZonedDateTime.parse("01-21-2013",fromFormatter.withZone(id));

2 Comments

Thank you for showing how to use java.time, the modern Java date and time API introduced since Java 8. It’s definitely recommended.
I fixed a couple of errors in your first code snippet. You need to use LocalDate for a date without time of day. I didn’t get the point in your last snippet, and it does not work, you may consider deleting it.
0

you can use this below code snippet

 public static String formatDate (String date, String initDateFormat, String endDateFormat) throws ParseException {

    Date initDate = new SimpleDateFormat(initDateFormat).parse(date);
    SimpleDateFormat formatter = new SimpleDateFormat(endDateFormat);
    String parsedDate = formatter.format(initDate);

    return parsedDate;
}

4 Comments

yes it will give me a string in 'mm-dd-yyyy' format, I know how to convert the string format to date also, but then how to convert this date which is in 'mm-dd-yyyy' format to the 'yyyy-mm-dd' date format which mysql wants
@Furquan now just chec kmy latest answer
yes, but then I want the date in DATE type object , is it possible or am I just wasting time
@Furquan you may look a similar question's answer here
0
Date initDate = new SimpleDateFormat("yyyy-MM-dd").parse("2015-03-05");
System.out.println("initDate == "+initDate);

output ==>> initDate == Thu Mar 05 00:00:00 ICT 2015

Comments

0

In HQL simple SQL function work :

DATE_FORMAT(DATE,'%d-%m-%Y') 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.