I have to compare 2 dates which are in String format as: Fri Aug 23 17:03:19 IST 2013
I am trying to use new SimpleDateFormat("dd-MM-yyyy HH-mm-ss") to convert in DateTime so that dates can be comparable .
tempTimeStamp=Fri Aug 23 17:03:19 IST 2013
DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss");
Date startDate;
startDate = df.parse(tempTimestamp);
String newDateString = df.format(startDate);
But its showing error object of this type can't be converted in DateFormat.Please help..
You were using the wrong format to try and parse the datetime string, try using the following snippet:
String tempTimeStamp="Fri Aug 23 17:03:19 IST 2013"
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date startDate = startDate = df.parse(tempTimestamp);
String newDateString = df.format(startDate);
You can look up more on the patterns for defining the date time format at http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
Your expected format doesn't matches with the one provided.
The format of your SimpleDateFormat does not match that of the String that you are using.
The input String should match dd-MM-yyyy HH-mm-ss
If you are wanting simply to format the output of a Date, then no parsing of a String is necessary.
Related
I want to convert a string to date before storing it and I used
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
Date returnDate = format.parse(date);
When I ran this with sample date:
the input string for date conversion is 2014-05-06
the parsed date is Mon Jan 06 00:05:00 IST 2014
now when I store the returnDate in MySql the value is 2014-01-06 00:05:00
Why is the date changed ? Want to know if I am missing something. I went through the posts related to date string conversion : How to convert a date from a Datepicker to Mysql DATETIME format using java?
In your DateFormat use MM for month instead of mm, that is for minutes
Reference: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
You can use like this :
Date mDate= new Date(System.currentTimeMillis());
SimpleDateFormat mDateFormat= new SimpleDateFormat("dd MMM yyyy HH:mm a");
String dateformat=mDateFormat.format(mDate);
the string ["dd MMM yyyy HH:mm a"] can be changed according to need of formate.
Like in your case : "yyyy-mm-dd
I have a date time (which is a string) in the following format: 2/19/2015 5:25:35 p.m, and I wanted to turn it in the following Date Format: Thu Feb 19 5:25:35 p.m. CET 2015 I tried the following code:
String sDatePrecedenteExecution = "19/02/2015 17:30:29";
SimpleDateFormat format = new SimpleDateFormat ("ddd d mmm yyyy HH: mm: ss");
Date date = format.parse (sDatePrecedenteExecution)
but I got the following error:
java.text.ParseException: unparseable Date: "2/19/2015 5:30:29 p.m."
Has java.text.DateFormat.parse (DateFormat.java:337)
You are currently using the "output" format to read your incoming date string (2/19/2015 5:25:35 p.m), which is why you see the error.
You need to specify a second format for parsing your incoming date string, and use that format to parse instead. It should look like this:
SimpleDateFormat inFormat = new SimpleDateFormat ("dd/MM/yyyy HH:mm:ss")
Date date = inFormat.parse(sDatePrecedenteExecution)
Note that you also have a bug in your output format - m means minutes, and you want MMM, which is months. Have a look at the docs.
Your SimpleDateFormat doesn't match the format which you are entering. They should reflect the same.
Try this code
String parseDate = ""19/02/2015 17:30:29";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date parsedDate = dateFormat.parse(parseDate);
You need to change your code something like...
String sDatePrecedenteExecution = "19/02/2015 17:30:29";
SimpleDateFormat format = new SimpleDateFormat ("dd/mm/yyyy HH:mm:ss");
try {
Date date = format.parse (sDatePrecedenteExecution);
System.out.println(date);
format = new SimpleDateFormat ("ddd d mmm yyyy HH: mm: ss");
String str = format.format(date);
System.out.println(str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Try this pattern:
SimpleDateFormat format = new SimpleDateFormat ("dd mm yyyy HH:mm:ss");
You went wrong when you made: "ddd d mmm yyyy HH: mm: ss"
Use this pattern "dd/M/yyyy HH:mm:ss" instead & read the documentation
SimpleDateFormat format = new SimpleDateFormat ("dd/M/yyyy HH:mm:ss");
String sDatePrecedenteExecution = "19/02/2015 17:30:29";
try{date =format.parse (sDatePrecedenteExecution);
}catch(Exception ex){//deal with it here}
System.out.println(date.toString()); //Thu Feb 19 17:30:29 UTC 2015
Actually I wanted to convert one String to date and then I need to format it in to one another format..
The String I have is
String val="Wed Jan 08 08:49:13 GMT+05:30 2014";
To convert it to
2014-01-30 10:14:18 , this format
Is it possible to do this I tried some method like
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c.getTime());
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+1:00"));
// Date currentLocalTime = ;
DateFormat date = new SimpleDateFormat("HH:mm:ss:mmss a");
But nothing worked
Your format string for parsing doesn't match the example date you have given.
You could try something like the following:
String val = "Wed Jan 08 08:49:13 GMT+05:30 2014";
DateFormat inFmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
Date date = inFmt.parse(val);
DateFormat outFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
outFmt.setTimeZone(TimeZone.getTimeZone("GMT+1:00"));
System.out.println(outFmt.format(date));
Note the following correspondences in the first two lines:
Wed -> EEE
Jan -> MMM
08 -> dd
etc.
All,
I need to convert the following string "Tue Jan 01 08:00:00 CET 2013" to a date object with format "dd/MM/yyyy HH:mm".
What I have done till now...
String dateStr = "Tue Jan 01 08:00:00 CET 2013";
DateFormat readFormat = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
DateFormat writeFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Date date = null;
try
{
date = readFormat.parse(dateStr);
} catch ( ParseException e )
{
e.printStackTrace();
}
String formattedDate = "";
if( date != null )
{
formattedDate = writeFormat.format(date);
}
System.out.println(formattedDate);
But this gives me a String as a result and not a date. If I parse the formattedDate String again using the writeFormat then I get the same original date back again i.e. Tue Jan 01 08:00:00 CET 2013.
NOTE: Finally, I want to push the date into MySQL DateTime datatype via Java Date object. i.e. String -> Java Date -> MySQL Date Time.
I have searched High/Low on the web and could not find a proper solution. Please help!!!
Thanks and regards,
SG
I found the answer.
DateFormat readFormat = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
date = readFormat.parse(string);
And then while saving to MYSQL database...
pstmt.setDate(5, new java.sql.Date(change.getEndDateTime().getTime()));
Thanks for all your responses and apologies for posting duplicate if it is...
Regards,
SG
I have a date string:
Thu Feb 20 08:00:00 EET 1992
And using this code to format it:
String datePatternFrom = "EEE MMM dd HH:mm:ss ZZZ yyyy";
String datePatternTo = "MMM dd, yyyy";
String prettyDate = "";
try {
DateFormat fromFormatter = new SimpleDateFormat(datePatternFrom);
Date date = (Date)fromFormatter.parse(userBirthday.toString());
DateFormat toFormatter = new SimpleDateFormat(datePatternTo);
prettyDate = toFormatter.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
Why I'am getting the exception?
java.text.ParseException: Unparseable date: "Thu Feb 20 08:00:00 EET 1992" (at offset 0)
The problem is with the weekday and month and your locale.
Thu is English, so you have to tell the parser that it should use English weekdays:
DateFormat fromFormatter = new SimpleDateFormat(datePatternFrom, Locale.US);
This will work for your pattern.
If you do not specify a locale, the default will be used, which is not always an English one. ;-)
It could be your locale. Try making a SDF with datePatternFrom, give it a date to format and print that somewhere. See what pops up.
Probably your userBirthday object was not created as a java.util.Date object. Can you try a System.out.println(userBirthday.getClass().getName());?