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.
Please help !!!
{
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);
}
}
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
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;
}
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
In HQL simple SQL function work :
DATE_FORMAT(DATE,'%d-%m-%Y')
Related
I am trying to format the following input date: "2019-02-12 18:00:40""
to the following format "dd-MM-yyyy". However, I am experiencing mixed results with the date formatter method I created below and the output is as follows
"Wed Aug 11 00:00:00 GMT+02:00 17"
private String formatDate(String dateT) throws ParseException
{
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date date = formatter.parse(dateT);
return date.toString();
}
As mentioned, you'll need two formats to get your desired result.
If you can use Java8+, I suggest using LocalDateTime and DateTimeFormatter (instead of SimpleDateFormat):
String stamp = "2019-02-12 18:00:40";
LocalDateTime ldt = LocalDateTime.parse(stamp, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(ldt.format(DateTimeFormatter.ofPattern("dd-MM-yyyy")));
Output:
12-02-2019
Edit:
If you really must use the outdated classes, you can apply the same principle with SimpleDateFormat:
String stamp = "2019-02-12 18:00:40";
SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dt1.parse(stamp);
SimpleDateFormat dt2 = new SimpleDateFormat("dd-MM-yyyy");
System.out.println(dt2.format(date));
As suggested by #Robert. This was the solution I ended up using with two simpledateformatters.
private String formatDate(String date) throws ParseException {
SimpleDateFormat inputDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currentDate = inputDate.parse(date);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String formattedDate = formatter.format(currentDate);
return formattedDate;
}
I want to convert the system date to yyyy-MM-dd format. There are similar questions in SO. I found that I need to parse the date in input format and then convert to the output format. But I am stuck at the first stage itself. I am not able to parse the system date as such (Sat Apr 25 14:44:15 IST 2015).
Here is my MWE:
import java.util.*;
import java.text.*;
public class Test
{
public static void main(String[] args)
{
try
{
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MM dd HH:mm:ss aaa YYYY");
date = dateFormat.parse(date.toString());
System.out.println(date);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
I get the exception as :
Unparseable date: "Sat Apr 25 14:53:33 IST 2015"
Date object can be converted to string of any date format.
String can be converted to date but it will come only in standard date format's but cant be in the one as you want..
If you want to format system date to yyyy-MM-dd format then use:
Date date = new Date();
SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
String date1 = dateFormater.format(date);
As you specified in comment you want to subtract sql date with current date then just convert the sql date to normal date format.
Like this:
String date = your date;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = dateFormat.parse(date);
Date currentdate = new Date();
Then use calender objects:
Calendar calendar = Calendar.getInstance();
calendar.setTime(date1);
Calendar calendar2 = calendar.getInstance();
calendar2.setTime(currentdate);
long difference = (calendar2.getTimeInMillis() - calendar
.getTimeInMillis()) / 60000;
This will give you the difference between two dates in minutes.
This will work for you
public class Test1 {
public static void main(String[] args) {
try
{
Date date = new Date();
System.out.println(date);
String dateFormat = new SimpleDateFormat("EEE MM dd HH:mm:ss aaa YYYY").format(date);
System.out.println(dateFormat);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
Output
Sat Apr 25 15:10:38 IST 2015
Sat 04 25 15:10:38 PM 2015
I think you should do it like that.
Date date = new Date();
String formattedDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
date = format.parse(formattedDate);
System.out.println(date);
But you should understand difference between "date" and "date format".
How can I format a :
Tue May 21 00:00:00:00 GMT +200 14 <--- Tue May 21 00:00:00:00 GMT
+200 2014
i tried :
StringBuilder myName = new StringBuilder(datum);
myName.setCharAt(datum.length()-4, '2');
myName.setCharAt(datum.length()-3, '0');
Date date= null; DateTimeFormat.getFormat("-- idk ----").parse(myName.toString()); Window.alert(myName.toString());
but i dont know how to define the same date format as the Date class
i think this isnt a good solution is there a better?
I suppose that datum is a Date object.
So, just do:
DateFormat df = new SimpleDateFormat("EEE MMM d HH:mm:ss:SS z yyyy");
System.out.println(df.format(datum));
More info: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html.
// getFormatedDate("in which pattern you are sending date", "how we want", date in string form);
example : formatedDate = getFormatedDate("yyyy-MM-dd", "ddMMyyyy", "2014-05-21");
private String getFormatedDate(String baseFormat, String reqFormat, String dateStr) {
String formatedDate = null;
try {
DateFormat fromFormat = new SimpleDateFormat(baseFormat);
fromFormat.setLenient(false);
DateFormat toFormat = new SimpleDateFormat(reqFormat);
toFormat.setLenient(false);
java.util.Date date = fromFormat.parse(dateStr);
formatedDate = toFormat.format(date);
} catch (ParseException ex) {
}
return formatedDate;
}
You have to use date formatter.
Your date formatter should be
DateFormat dateFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss:SS z yyyy");
Then use this formatter to get your desired date.
Just do:
newDate = dateFormat.format(yourOldDate);
Hope this will works.
Get more from:
http://www.mkyong.com/java/java-date-and-calendar-examples/
and http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
Thanks.
I am trying to use simple date format to format the current time:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Date now = new Date(System.currentTimeMillis());
try {
java.util.Date formattedDate = sdf.parse(now.toString());
System.out.println(formattedDate.toString());
} catch (ParseException e) {
e.printStackTrace();
}
However the output I am getting is:
Fri Oct 18 00:00:00 CDT 2013
I am trying to achieve:
2013/08/18
What am I doing wrong?
You have to use the SimpleDateFormat#format(Date date) method, which returns the date formatted in the desired way. Note that this method is inherited from DateFormat.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Date now = new Date(System.currentTimeMillis());
String formattedDate = sdf.format(now);
System.out.println(formattedDate);
You are parsing your Date but not formatting it with your SimpleDateFormat. The Date#toString() method has its own output format. Use your SimpleDateFormat instead.
System.out.println(sdf.format(formattedDate));
You don't need to call parse
You don't need try/catch
Try this much simpler code:
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
String formattedDate = sdf.format(new Date());
System.out.println(formattedDate);
You can try using this
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
public class DateFormatTest {
public static void main(String[] args) {
Date now = new Date();
// Use Date.toString()
System.out.println(now);
// Use DateFormat
DateFormat formatter = DateFormat.getInstance(); // Date and time
String dateStr = formatter.format(now);
System.out.println(dateStr);
formatter = DateFormat.getTimeInstance(); // time only
System.out.println(formatter.format(now));
// Use locale
formatter = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.FRANCE);
System.out.println(formatter.format(now));
// Use SimpleDateFormat
SimpleDateFormat simpleFormatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println(simpleFormatter.format(now));
}
}
I am using jdk- 1.6.
I am try to parse String "24-10-2012" date to Date (24-10-2012) but i am getting this error:
java.text.ParseException: Unparseable date: "18-11-2012"
java.text.DateFormat.parse(DateFormat.java:354)
I am parsing like this:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
Date date = formatter.parse(currentDate);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
System.out.println(formatter.parse(currentDate));
prints
Wed Oct 24 00:00:00 CEST 2012
Your problem cannot be reproduced with the code you have posted.
My hypothesis: your exception is thrown from a piece of code other than the one you are accusing of the error. You could try carefully analyzing the stack trace in order to track down the real culprit.
Date in java does not hold any format. Read more...
When I run
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
Date date = formatter.parse(currentDate);
System.out.println(date);
System.out.println(formatter.format(date));
I get
Wed Oct 24 00:00:00 BST 2012
24-10-2012
which is as I expected. Can you clarity what the problem is?
You can use this for the format "dd-mm-yyyy"
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
Date date = formatter.parse(currentDate);
System.out.println(formatter.format(date));
import java.util.Date;
import java.text.SimpleDateFormat;
public class SimpleFormatDate
{
public static void main(String args[]){
Date todaysDate = new java.util.Date();
// Formatting date into yyyy-MM-dd HH:mm:ss e.g 2008-10-10 11:21:10
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = formatter.format(todaysDate);
System.out.println("Formatted date is ==>"+formattedDate);
// Formatting date into yyyy-MM-dd e.g 2008-10-10
formatter = new SimpleDateFormat("yyyy-MM-dd");
formattedDate = formatter.format(todaysDate);
System.out.println("Formatted date is ==>"+formattedDate);
// Formatting date into MM/dd/yyyy e.g 10/10/2008
formatter = new SimpleDateFormat("MM/dd/yyyy");
formattedDate = formatter.format(todaysDate);
System.out.println("Formatted date is ==>"+formattedDate);
}
}
output
Formatted date is ==>2008-10-10 13:03:54
Formatted date is ==>2008-10-10
Formatted date is ==>10/10/2008
Wait a second.. Why u need to parsing that if u have a right value ?
Anyway, i use this :
SimpleDateFormat oFormat = new SimpleDateFormat("yyyy-MM-dd");
String sDate = oFormat.format("24-10-2012");
it will appearing date like 2012-10-24. So if u want to parsing to dd-MM-yyyy, u just need change the format to what u want.
NB : Sorry if my english is bad. :D