Reputation: 977
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.
Upvotes: 3
Views: 7767
Reputation: 77
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));
Upvotes: 1
Reputation: 3789
In HQL simple SQL function work :
DATE_FORMAT(DATE,'%d-%m-%Y')
Upvotes: 0
Reputation: 1
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
Upvotes: 0
Reputation: 2409
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
Upvotes: 2
Reputation: 2655
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;
}
Upvotes: 0