I'm receiving a date as a String like this :2015-07-22.06.05.56.344. I wrote a parsing code like this
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd.hh.MM.ss.ms");
try {
Date date = sdf.parse("2015-07-22.06.05.56.344");
System.out.println(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
And I got the output like this:
Fri May 22 06:03:44 IST 2015
Why is it reading it wrongly? Is it an issue with my code or java cannot recognize this date format?
Your MM/mm are around the wrong way, mm is for "Minute in hour" and MM is for "Month in year"
SS is for "Millisecond" (or ms, which means nothing)
I'd also recommend using HH instead of hh as HH is for "Hour in day (0-23)"
So, using...
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd.HH.mm.ss.SS");
It outputs Wed Jul 22 06:05:56 EST 2015 for me
You need to use MM for months and mm for minutes.
Try setting SimpleDateFormat to this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd.hh.mm.ss.SSS");
Check javadoc for more info:
SimpleDateFormat
You have the wrong pattern. The pattern is case sensitive.
mm stands for minutes
MM stand for month.
SS is miliseconds
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd.hh.mm.ss.SSS");
For more details see the SimpleDateFormat documentation
Related
I'm trying to parse the following string to a Date object:
2013-12-26T01:00:56.664Z
Using this SimpleDateFormat:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
But I'm getting a:
java.text.ParseException: Unparseable date: "2013-12-26T01:00:56.664Z" (at offset 19)
What am I doing wrong, How I should handle the T and the Z letters in the date?
The real isssue with the date is not T & Z but the milliseconds.
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" This must be the format that is to be used becaue there are milli seconds as well in the input date.
You can use this
String date = "2013-12-26T01:00:56.664Z";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
try {
System.out.println(sdf.parse(date)); // Result Thu Dec 26 01:00:56 CET 2013
} catch (ParseException e) {
e.printStackTrace();
}
Why is this code giving me trouble?
public Date setupDate(){
String startDateString ="05/10/2010 04:30:20";
SimpleDateFormat df = new SimpleDateFormat("mm/dd/yyyy HH:mm:ss");
Date startDate = null;
try {
startDate = df.parse(startDateString);
String newDateString = df.format(startDate);
System.err.println(newDateString);
System.err.println(startDate.toString());
} catch (ParseException e) {
e.printStackTrace();
}
return startDate;
}
output:
SEVERE: 30/10/2010 04:30:20
SEVERE: Sun Jan 10 04:30:20 EST 2010
I expected May 10 of course, not January(I don't know how it became January, or the 30.
Read the section Date and Time Patterns
You should use
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
M is for Month in year while m is for minute in hour
The format symbol for month uses capital M; you've used minutes m twice, which is 30 here. For reference, here's the Javadocs that explain all format symbols for SimpleDateFormat.
I have some data which contains date in different formats, eg: yyyy-dd-MM, yyyy-MM-dd, EEE dd-MM-yy etc.
I am trying to find a way to differentiate between dd-MM-yyyy and MM-dd-yyyy.
I understand that if dd is less than 12, there is no way I can be sure about format, However by identifying other cases when dd > 12, I can minimize the the wrong calculation.
I tried this -
SimpleDateFormat targetFormat = new SimpleDateFormat("EEE, yyyy-MMM-dd hh:mm:ss a");
SimpleDateFormat originalFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat originalFormat2 = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss");
Calendar cal = Calendar.getInstance();
try {
Date date = originalFormat1.parse(s); //I tried with s = "2013-25-8 20:10:00";
cal.setTime(date);
if (cal.get(Calendar.DAY_OF_MONTH) > 12)
date = originalFormat2.parse(s);
System.out.println(targetFormat.format(date));
} catch (ParseException e) {
System.out.println("Error");
Output
I was expecting : Sun, 2013-Aug-25 08:10:00 PM
But I got : Thu, 2015-Jan-08 08:10:00 PM
You can try:
originalFormat1.setLenient(false);
before you try to parse a string with it; that should make it throw a ParseException when the month number is out of range.
When you apply format 1 and it interprets 25 as month then the date starts to become weird, that makes sense as month cannot be bigger than 12. Therefore, your if statement doesn't make sense. You have to check the format before applying the SimpleDateFormat (for instance with Integer.parseInt(s.substring(5,7) > 12).
Hey actually it is taking 25 as month, So 12 + 12 + 1 means 2 years and one month i.e "January"
So, your date becomes : "Thu Jan 08 20:10:00 GMT 2015" and again when you are changing it into "targetFormat" , this unusual result ensues...
This question already has answers here:
Date Format JAVA
(5 answers)
Closed 9 years ago.
I have a date in the following format
//input date
Thu Jun 06 2013 00:00:00 GMT+0530 (India Standard Time)
//output date format
I want to change this to "dd-mm-yyyy, hh:mm:ss".
I get the input date format from db. I have to change that into output date format which i will be showing it in a grid.
I tried the following code.
DateFormat outputDate = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss");
try
{
Date date = outputDate.parse(facade.getDate.toString()); **//getting exception here**
outputDate = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss");
Date date1 = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss").parse(outputDate
.format(date));
facade.setDate(date1);
}catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
I am getting
java.text.ParseException: Unparseable date: "2013-06-06 00:00:00.0".
Any help..
"2013-06-06 00:00:00.0" does not match "dd-mm-yyyy, hh:mm:ss" your format should be "dd-MM-yyyy hh:mm:ss" instead
But, looking at your code I'm guessing facade.getDate is actually a java.sql.Timestamp which inherits from java.util.Date so you can directly pass it to the format like so
new SimpleDateFormat("dd-MM-yyyy, hh:mm:ss").format(facade.getDate)
Here's some code which works for me:
import java.text.*;
import java.util.*;
public class Test {
public static void main(String[] args) throws Exception {
String input = "Thu Jun 06 2013 00:00:00 GMT+0530 (India Standard Time)";
DateFormat inputFormat = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss 'GMT'z",
Locale.ENGLISH);
Date date = inputFormat.parse(input);
DateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss",
Locale.ENGLISH);
outputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String output = outputFormat.format(date);
System.out.println(output);
}
}
Things to consider:
You need to work out your output time zone. Currently I've got it set to UTC, but that may not be what you want.
You really need to take a step back and think things through. You've clearly got two different formats - you're trying to convert from one to the other. So creating three different SimpleDateFormat objects all with the same format is never going to work.
You need to read documentation carefully... in SimpleDateFormat, M means month and m means minute; h uses the 12-hour clock and H uses the 24-hour clock.
This is assuming you actually need to start with a string though. If getDate is already a Date or a Timestamp, you can ignore the first part - just use the output part of the above code. You should avoid unnecessary string conversions wherever possible.
Note that dd-MM-yyyy is a slightly unusual format - are you sure you don't actually want yyyy-MM-dd which is more common (and sortable)?
DateFormat outputDate = new SimpleDateFormat("yyyy-dd-mm hh:mm:ss");
try {
Date date = outputDate.parse("2013-06-06 00:00:00.0");
System.out.println(new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss").format(date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
works well, line 1 was incorrect. Your SimpleDateFormat.parse needs to be in the exact format of the input date. Then you want to output it in a different format so you make another one and set the format then call SimpleDateFormat.format(date) and I put a println on it.
Fault is here
DateFormat outputDate = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss");
pattern should be equals to Thu Jun 06 2013 00:00:00 GMT+0530 (India Standard Time). not to your out put strings pattern.
#Test
public void test() throws ParseException {
SimpleDateFormat sdf_org = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.ENGLISH);
Date d = sdf_org.parse("Thu Jun 06 2013 00:00:00 GMT+0530");
SimpleDateFormat sdf_target = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss.SSS");
System.out.println(sdf_target.format(d));
}
output console : 2013-30-06 03:30:00.000
I have the following code:
SimpleDateFormat format=new SimpleDateFormat("yyyy.MM.dd HH:mm");
try {
Date date=format.parse("2012.9.11 02:00");
Log.i("date", date.toGMTString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("exception", e.getMessage());
}
But I've got the message: "10 Sep 2012 22:00:00 GMT", i.e. incorrect date. How can I fix it?
That IS the correct date. It is calculated based on your GMT offset.
To print it like this 11-Sep-2012 02:00:00 use date.toLocaleString() (deprecated method)
Or you can print the date using the Calendar class using:
SimpleDateFormat format=new SimpleDateFormat("yyyy.MM.dd HH:mm");
Date date=format.parse("2012.09.11 02:00");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
System.out.println(format.format(cal.getTime()));
This prints : 2012.09.11 02:00
You can see http://www.vogella.com/articles/JavaDateTimeAPI/article.html for more info
You're printing date.toGMTString() and so you get "10 Sep 2012 22:00:00 GMT" which isn't an incorrect string but the date in Greenwich Mean Time. Note that toGMTString is deprecated.
If you want to print your date in your format, you may do
Log.i("date", format.format(date));
If you don't want to get supplementary "0", use
format=new SimpleDateFormat("yyyy.M.d HH:mm");
The method toGMTString() is deprecated.
However, as for an explanation, you might be in different time zone, thus have a different locale.
Try replacing with date.toLocaleString() should output correctly, although also a deprecated method.
As Per JavaDoc, date.toGMTString() is a deprecated method.
Its not good practice to use deprecated methods.
Use date.toString() instead of date.toGMTString().
Or
format.format(date);
It is incorrect because you're using date.toGMTString()
Just try to output the variable date and you'll able to see the correct time!