I have a given String 2019-04-17 10:00:43+02:00 and want to convert it to something like Mon Apr 15 12:05:47 CEST 2019
I tried:
String date = "2019-04-17T11:02:46+02:00"
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-mm-dd'T'hh:mm:ssX ");
java.util.Date result = formatter.parse(date);
but it gives an Exception like below.
Exception in thread "main" java.text.ParseException: Unparseable date:
"2019-04-17 11:02:46+02:00"
at java.text.DateFormat.parse(Unknown Source)
This is a default ISO format which can be parsed as is with OffsetDateTime:
String date = "2019-04-17T11:02:46+02:00";
OffsetDateTime odt = OffsetDateTime.parse(date);
If you really need a java.util.Date, you can then use:
Date legacyDate = Date.from(odt.toInstant());
the problem with your code is you have put a extra space in the formatter.
String date = "2019-04-17T11:02:46+02:00";
DateFormat format = new SimpleDateFormat("yyyy-mm-dd'T'hh:mm:ssX");
Date rst = format.parse(date);
System.out.println(rst);
output :
Thu Jan 17 14:32:46 IST 2019
Related
I have Strings representing dates with the format 2014-11-01T18:57:24.497Z which I want to parse
as SimpleDateFormat.
I am using the following code
// 2014-11-01T18:57:24.497Z
SimpleDateFormat startAnalyzing = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz");
Date start = startAnalyzing.parse(startDateAnalyzing);
When doing this I am getting an exception:
java.text.ParseException: Unparseable date: "2014-11-01T18:57:24.497Z"
at java.text.DateFormat.parse(DateFormat.java:357)
...
What I am doing wrong?
Firstly you are trying to parse a z with Z so either choose z lower or upper for both (the String and the pattern).
Secondly, you need to "escape" the Z in the pattern (or z).
String startDateAnalyzing = "2014-11-01T18:57:24.497z";
SimpleDateFormat startAnalyzing = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'z'");
Output:
Sat Nov 01 18:57:24 CET 2014
I am new to java. I have a Date that is stored in the variable, pubDate = "2013-09-23"
When I'm executing this
SimpleDateFormat pubSimpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
Date publishDate = pubSimpleDateFormat.parse(pubDate);
I'm getting wrong value : Wed Jan 23 00:09:00 GMT+05:30 2013
Please help me why it so. and help me to solve this.
M is for Month in year while m is for Minute in hour
You should use SimpleDateFormat pubSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String pubDate = "2013-09-23";
SimpleDateFormat pubSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date publishDate = pubSimpleDateFormat.parse(pubDate);
System.out.println(publishDate);
Output :
Mon Sep 23 00:00:00 GMT 2013
Read the section Date and Time Patterns.
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.
DateFormat formatter = new SimpleDateFormat("yy-mm-dd");
formatter.setLenient(false);
String[] dateStr = { "2013-12-27", "2013-01-03"};
for (int i = 0; i <= 1; i++) {
Date date = formatter.parse(dateStr[i]);
System.out.println("date is "+date);
}
result :
Sun Jan 27 00:12:00 IST 2013
Thu Jan 03 00:01:00 IST 2013
i am parsing string date in to Date.but it is giving me date Starting with month Jan regardless of what month i am passing to formatter constructor.
The format for your date would be yy-MM-dd. Update your format and check.
mm for minutes
MM for month
Use: "yy-MM-dd"
See here
once Silly Mistake
DateFormat formatter = new SimpleDateFormat("yy-MM-dd");
Format this line in your code
Unix timestamp is 1334672401.
long t = Long.parseLong(map.get("timestamp").toString());
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd, ''yy, hh:mma");
Date time = new Date(t*1000);
Date date = formatter.parse(time.toString());
I'm trying to convert a unix timestamp into a Date object in the format to something similar to Thu Apr 17 2012, 16:25 but I keep getting an unparseable date error and I'm not sure whats wrong exactly?
You have the Date object already when you did this:
Date time = new Date(t*1000);
Use the formatter to format your string output, like this:
System.out.println(formatter.format(time));