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".
Related
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')
I want return calendar type object but while parsing from String to Date type,Its changing the format.
public static Calendar Dateandtime(Timestamp timeStamp) throws ParseException {
Calendar tempCal = new GregorianCalendar();
SimpleDateFormat ft = new SimpleDateFormat ("yyyy.MM.dd 'T' hh:mm:ss.SSS z");
String date1 = ft.format(timeStamp.getTime());
System.out.println("***String date ***"+date1);
Date d = ft.parse(date1);
tempCal.setTime(d);
System.out.println("*****Date *****"+tempCal.getTime());
System.out.println("*****Calendar *****"+tempCal);
return tempCal;
}
Ouput
***String date ***2014.09.11 T 03:58:25.00 IST
*****Date *****Thu Sep 11 03:58:25 IST
*****Calendar****java.util.GregorianCalendar[time=1410388105000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=8,WEEK_OF_YEAR=37,WEEK_OF_MONTH=2,DAY_OF_MONTH=11,DAY_OF_YEAR=254,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=3,HOUR_OF_DAY=3,MINUTE=58,SECOND=25,MILLISECOND=0,ZONE_OFFSET=19800000,DST_OFFSET=0]
But I want this format 2014.09.11 T 03:58:25.00 IST.
How to achieve this using above code any help regarding this.
Where as my output should be 2014.09.11 T 03:58:25.00 IST
The calendar object does not hold formatting information.
As per your code, you have already obtained the data in your required format, from the following lines:
SimpleDateFormat ft = new SimpleDateFormat ("yyyy.MM.dd 'T' hh:mm:ss.SSS z");
String date1 = ft.format(timeStamp.getTime());
System.out.println("***String date ***"+date1);
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 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
Can any one help in conversion of date format?
My returned date object is contains "Mon Jul 12 00:00:00 IST 2010"
I'm trying to convert this date format to "MM/dd/yyyy" but I'm getting parse exception. Please help me how to convert it
Code from the OP's comment:
String mydatObj = myDate.toString();
Date formatedDate = getDateFormat(mydatObj);
public static Date getDateFormat(String dateString) {
Date date = null;
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
try {
// set isLenient to false to adhere to the date format.
format.setLenient(false);
date = format.parse(dateString);
} catch (ParseException parseException) {
// ignore
LOG.error(parseException.getMessage(), parseException);
}
return date;
}
Well, you are getting a ParseException since you are trying to parse a date with the wrong format.
Here is a small code snippet which will work with the format you have:
// parse the date
DateFormat f = new SimpleDateFormat("E MMM dd HH:mm:ss zzz yyyy");
Date d = f.parse("Mon Jul 12 00:00:00 IST 2010"); // works
// now print the date
DateFormat out = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(out.format(d));