Issue with parsing Date string - java

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();
}

Related

(Android) How to convert a time raw string (with timezone) to another string format with the same timezone

I have a time raw string, "2016-05-15T12:42:00.000-04:00" and I want to convert the string to "Wed 15 May 2016 12:42", which keeps the same timezone (-04:00) as its original source.
I have tried SimpleDateFormat but using it returns different timezones that are not the same as the timezone in my original string. Please help me achieve this in Android Studio!
Other examples:
2016-05-15T15:42:00.000-08:00 -> Wed 15 May 2016 15:42
2016-05-15T14:44:00.000-01:00 -> Wed 15 May 2016 14:44
public static String formatDateString(String originDateString) {
//Original format 2016-05-15T12:42:00.000-04:00
SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
SimpleDateFormat resultFormat = new SimpleDateFormat("EEE dd MMM yyyy HH:mm");
String dateString = "";
try {
Date date = originalFormat.parse(originDateString);
dateString = resultFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
//returned format: Tue 14 May 2016 12:42
return dateString;
}
try saving it first without the TZ information
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");

java date format yyyy-mm-dd.hh.MM.ss.ms

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

Java date object returning 30 as the month?

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.

Exception in parsing date format [duplicate]

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

Java - Parsing a Date from a String

I want to parse a java.util.Date from a String. I tried the following code but got unexpected output:
Date getDate() {
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd");
try {
date = sdf.parse("Sat May 11");
} catch (ParseException ex) {
Logger.getLogger(URLExtractor.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
return date;
}
When I run the above code, I got the following output:
Mon May 11 00:00:00 IST 1970
You have not specified a year in your string. The default year is 1970. And in 1970 the 11th of May was a Monday - SimpleDateFormat is simply ignoring the weekday in your string.
From the javadoc of DateFormat:
The date is represented as a Date object or
as the milliseconds since January 1, 1970, 00:00:00 GMT.
Specify a year within the Format to get the correct output.
If you don't specify any year, the default is 1970.
if the year is the problem you can add y for year:
public Date getDate() {
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd y");
try {
date = sdf.parse("May 11 2010");
} catch (ParseException ex) {
Logger.getLogger(URLExtractor.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
return date;
}
System.out.println(getDate());
Tue May 11 00:00:00 EDT 2010
Edit:
To get the correct day of the week you need to specify the date (with the year). I edited the code above.

Categories