ParseDate Unparseable - java

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

Related

Convert given String to a Date object

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

Remove specific words/strings from the list

How can we remove and preserve only Date from a String:
For ex: String outPut = {Time = 0:0:0} {Timestamp = Tue Oct 20 23:54:10 BST 2013}
I would like to have only Date
For ex: Tue Oct 20 2013
So far, I have tried the following approach:
String[] manipulateDate = output.getDate().split("\\{");
for(String s : manipulateDate ){
String outputDate = manipulateDate [2].replaceAll("\\}", "").replaceAll("\\s*\\bTimestamp =\\b\\s*","");
System.out.println(outputDate );
}
What is the best way to implement and use Java Date/String?
To strictly answer your question, you could do:
String date = output.replaceAll(".*Timestamp = (.*)? \\d{2}:.*?(\\d{4}).*", "$1 $2");
But it may be preferable to parse the whole string as a date object:
String output = "{Time = 0:0:0} {Timestamp = Sun Oct 20 23:54:10 BST 2013}";
String timestamp = output.replaceAll("\\{.*?\\} \\{Timestamp = (.*)\\}", "$1"); //Sun Oct 20 23:54:10 BST 2013
ZonedDateTime dateTime = ZonedDateTime.parse(timestamp, DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy", Locale.ENGLISH));
From that point on, you can do what you want with the date object, including printing it as a string. Note that I changed the original string to Sunday (vs. Tuesday in your question): parsing it as a date also caught the fact that the 20th of October 2013 was not a Thursday, another advantage over strings.

SimpleDateFormat function parse(String s) gives wrong date

As an input I have Date object(for example, exDate=Fri Aug 01 00:00:00 EEST 2014) that must be formated. After the parsing of the date, I get wrong date.
SimpleDateFormat sdf = new SimpleDateFormat(
"dd-MMM-YYYY hh.mm.ss.SSSSSSSSS aa", Locale.ENGLISH);
String dateStart = sdf.format(exDate);
Date dateF = sdf.parse(dateStart);
dateStart will be equal to
01-Aug-2014 12.00.00.000000000 AM
and the resut, dateF will be equal to
Sun Dec 29 00:00:00 EET 2013
So, after the parsing of a string with date, the result is wrong.
Maybe, somebody know the source of the problem? Or another way to format date in another SimpleDateFormat?
The problem is the YYYY which means:
Y Week year;
The actual year, which is what you are looking for would be yyyy.
I really recommend that you go in the link above to see the full list.
You should also replace the milliseconds to .SSS as you can't get more precise than that.

Working with Dates in Java

I need to work with dates and I wasn't sure how to go about that in Java since I have never done it before.
I am pulling dates from the Excel file and they can be retrieved in the Data format which would represent the date.
Ex:
2/1/2010
5/12/2011
8/15/2011
9/1/2011
9/1/2011
All my codes are pretty irrelvent to the question, but I am setting up a getter/setter method:
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
So my question is this, in what way when I am pulling the data from Excel:
temp.setDate((row.getCell(3).getDateCellValue());
I can set the limit so it only retrieves data from x amount of months. 8, 12, et.. from the last month/year displayed in the file, instead of going back all the way to 2010? I can provide more details if needed.
Edit: This is what I have now:
import java.util.Date;
Date date;
date = row.getCell(3).getDateCellValue();
It shows: Tue June 01 00:00:00
I don't care about Tuesday or 00:00:00, I just have a whole list of data and I only want to show x amount of months.
Edit: I figured it out. :)
The question is pretty vague, but if you are using Excel I have found it very beneficial to save the file as a 'filename.csv'. This format very easy to work with, it is comma delimited going across and newlines going down. If you are periodically checking the month, it would be easy to ensure that you only go x months backwards.
First you need to parse the dates accordingly using the SimpleDateFormat class. The result is a date object.
SimpleDateFormat df = new SimpleDateFormat( "M/d/yyyy" );
Date date = df.parse(row.getCell(3).getDateCellValue());
Then:
Calendar cal = Calendar.getInstance();
will return you an object ob type Calendar, where you can set the date by
cal.setTime(date);
Finally your loop reading the Excel file can determine according to the calendar object, if the date should be included / further processed by using:
int month = cal.get(Calendar.MONTH); // e.g. 11 for Nov
int year = cal.get(Calendar.YEAR); // e.g. 2011
You have to work with DateFormat.class fo parse the cell, or use a Calendar to put Day Month Year
EDIT
You can also use Calendar.class
final String[] tabDate = {"2/1/2010", "5/12/2011", "8/15/2011",
"9/1/2011", "9/1/2011"};
// Extract field's value
final Calendar c = Calendar.getInstance();
// Parse the list of stringDate
for (final String string : tabDate) {
System.out.println("Input string: " + string);
final String[] shortDate = string.split("/");
// Build Calendar
c.set(Integer.valueOf(shortDate[2]), Integer.valueOf(shortDate[0]),
Integer.valueOf(shortDate[1]));
// Extract date as you like
System.out.format("%25s : %d/%d/%d\t%s\n\n", c.getTime(),
c.get(Calendar.MONTH), c.get(Calendar.DATE),
c.get(Calendar.YEAR), c.getTimeInMillis());
}
Console :
Input string: 2/1/2010
Mon Mar 01 09:09:48 CET 2010 : 2/1/2010 1267430988109
Input string: 5/12/2011
Sun Jun 12 09:09:48 CEST 2011 : 5/12/2011 1307862588109
Input string: 8/15/2011
Thu Sep 15 09:09:48 CEST 2011 : 8/15/2011 1316070588109
Input string: 9/1/2011
Sat Oct 01 09:09:48 CEST 2011 : 9/1/2011 1317452988109
Input string: 9/1/2011
Sat Oct 01 09:09:48 CEST 2011 : 9/1/2011 1317452988109

Java - unparseable date, need format to match "GMT-0400"

I have the following Java:
DateFormat formatter = new SimpleDateFormat(
"EEE MMM dd yyyy HH:mm:ss zZ (zzzz)", Locale.ENGLISH);
Calendar cal = Calendar.getInstance();
cal.set(2011, Calendar.APRIL, 1);
out.println(formatter.format(cal.getTime()));
out.println();
Date date;
try {
date = formatter
.parse("Fri Apr 01 2011 00:00:00 GMT-0400 (Eastern Daylight Time)");
} catch (ParseException e) {
out.println("Failed to parse date: " + e.getMessage());
e.printStackTrace(out);
}
This is in a servlet, and the Calendar-constructed date comes out as:
Fri Apr 01 2011 16:42:24 EDT-0400 (Eastern Daylight Time)
This looks like the same format as the date string I'm trying to parse, except for EDT-0400 versus the desired GMT-0400. The code fails when trying to parse the date string:
java.text.ParseException: Unparseable date: "Fri Apr 01 2011 00:00:00 GMT-0400 (Eastern Daylight Time)"
How can I parse such a string? This is coming from a JavaScript date in a Sencha Touch 1.1.1 model, stored in WebSQL local storage.
For some reason GMT-0400 isnt' working, and UTC-0400 is working. You can replace GMT with UTC.
Note that this part will be completely ignored - the timezone will be resolved from what's found in the brackets (at least on my machine, JDK 6)
I debugged SimpleDateFormat and it seems that it will only parse GMT-04:00 but not GMT-0400.
It will accept UTC-0400, however it will throw away the hours/minutes modifier and will incorrectly parse it as UTC. (This happens with any other timezone designation, except for GMT)
It will also parse -0400 correctly, so the most robust solution is probably to simply remove GMT from your date string.
The upshot of the story is that SimpleDateFormat is anything but simple.
Update: Another lesson is that I could've saved a lot of time by passing a ParsePosition object to the parse() method:
DateFormat formatter = new SimpleDateFormat(
"EEE MMM dd yyyy HH:mm:ss zzzz", Locale.ENGLISH);
Date date;
ParsePosition pos = new ParsePosition( 0 );
date = formatter
.parse("Fri Apr 01 2011 00:00:00 UTC-0400", pos);
System.out.println( pos.getIndex() );
Will print out 28, indicating that the parsing ended at character index 28, just after UTC.

Categories