I use jodatime to parse date time strings as follows:
public static void main(String[]args){
String s ="16-Jul-2009 05:20:18 PDT";
String patterns = "dd-MMM-yyyy HH:mm:ss z";
DateTimeFormatter fm = DateTimeFormat.forPattern(patterns);
DateTime d=fm.parseDateTime(s);
System.out.println(d);
}
I get
Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "16-Jul-2009 05:20:18 PDT" is malformed at "PDT"
at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:683)
what's wrong? how to parse the timezone properly?
From the DateTimeFormat javadoc:
The pattern syntax is mostly compatible with java.text.SimpleDateFormat - time zone names cannot be parsed and a few more symbols are supported. All ASCII letters are reserved as pattern letters, which are defined as follows:
Your best bet is to fall back to SimpleDateFormat and then construct DateTime based on Date#getTime().
String s = "16-Jul-2009 05:20:18 PDT";
String pattern = "dd-MMM-yyyy HH:mm:ss z";
Date date = new SimpleDateFormat(pattern, Locale.ENGLISH).parse(s);
DateTime d = new DateTime(date.getTime());
System.out.println(d);
Related
I'm having date in String format as "2019-10-30 12:17:47". I want to convert this to an instance of Date along with the time so that I can compare two date obejcts.
This is what I've tried:
String dateString = "2019-10-30 12:17:47" //Date in String format
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); //sdf
Date d1 = format.parse(dateString);
But here I'm getting exception as "Unparseble date exception".
Kindly help...
What went wrong in your code?
In your format pattern string, yyyy-MM-dd HH-mm-ss, you have got two spaces between the date and the time. Since your date string, 2019-10-30 12:17:47, has got only one space there, your formatter objects by throwing the exception. This was also what Tim Biegeleisen said in a comment. The comment by deHaar is true too: The hyphens between hour, minute and second don’t match the colons in your date string either.
What to do instead?
See the good answer by deHaar
You should really switch to java.time (as already suggested in one of the comments below your question). It isn't more difficult than the outdated temporal classes from java.util but less error-prone and more powerful concerning offsets, time zones, daylight saving time and the multitude of different calendars the world has.
See this little example:
public static void main(String[] args) {
String dateString = "2019-10-30 12:17:47";
// define your pattern, should match the one of the String ;-)
String datePattern = "yyyy-MM-dd HH:mm:ss";
// parse the datetime using the pattern
LocalDateTime ldt = LocalDateTime.parse(dateString,
DateTimeFormatter.ofPattern(datePattern));
// print it using a different (here a built-in) formatting pattern
System.out.println(ldt.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
// or you just use the one defined by you
System.out.println(ldt.format(DateTimeFormatter.ofPattern(datePattern)));
// or you define another one for the output
System.out.println(ldt.format(DateTimeFormatter.ofPattern("MMM dd yyyy HH-mm-ss")));
}
The output on my system looks like this:
2019-10-30T12:17:47
2019-10-30 12:17:47
Okt 30 2019 12-17-47
The date in string you want to format does not match the formatter. See more detail here,
https://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html
#Test
public void test2() {
String dateString = "2019-10-30 12:17:47"; //Date in String format
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //sdf
try {
Date d1 = format.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
}
There are two ways to do it
first is your way
String dateString = "2019-10-30 12:17:47"; // Date in String format
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // sdf
Date d1 = format.parse(dateString
second is my way (Local date)
LocalDate resultDate = dateFormat("2019-10-30 12:17:47");
System.out.println(resultDate);
public static LocalDate dateFormat(String textTypeDateTime) {
final DateTimeFormatter dateTimetextFormatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return LocalDate.parse(textTypeDateTime, dateTimetextFormatter);
}
I'm trying to get the current DateTime with my DateTimeFormat pattern, but i'm getting the exception...
//sets the current date
DateTime currentDate = new DateTime();
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/YYYY HH:mm").withLocale(locale);
DateTime now = dtf.parseDateTime(currentDate.toString());
I'm getting this exception, I cannot understand who is giving the malformed format
java.lang.IllegalArgumentException: Invalid format: "2017-01-04T14:24:17.674+01:00" is malformed at "17-01-04T14:24:17.674+01:00"
This line DateTime now = dtf.parseDateTime(currentDate.toString()); isn't correct because you try parse date with default toSring format. You have to parse string which formatted the same way as pattern:
DateTime currentDate = new DateTime();
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/YYYY HH:mm").withLocale(locale);
String formatedDate = dtf.print(currentDate);
System.out.println(formatedDate);
DateTime now = dtf.parseDateTime(formatedDate);
System.out.println(now);
You are using the wrong format to parse the date. If you print out the date you are trying to parse after converting it to a String with toString you get:
2017-01-04T14:24:17.674+01:00
This date string does not conform to the pattern dd/MM/YYYY HH:mm. To parse the to a string converted currentDate to a DateTime object again, you have to use the following pattern:
DateTimeFormatter dtf = DateTimeFormat.forPattern("YYYY-MM-dd'T'HH:mm:ss.SSSZ")
.withLocale(locale);
Parsing with this DateTimeFormatter will get you another instance that represents the same time as the original currentDate.
For more details on the DateTimeFormatter and it's parsing options check out the JavaDoc
How to convert from "2014-06-16T07:00:00.000Z" to "16-JUN-14 07:00:00" using joda time API?
The below code is throwing the exception
java.lang.IllegalArgumentException: Illegal pattern component: T
at org.joda.time.format.DateTimeFormat.parsePatternTo(DateTimeFormat.java:570)
at org.joda.time.format.DateTimeFormat.createFormatterForPattern(DateTimeFormat.java:693)
at org.joda.time.format.DateTimeFormat.forPattern(DateTimeFormat.java:181)
at com.joda.JodaTimeTest.convertJodaTimezone(JodaTimeTest.java:59)
at com.joda.JodaTimeTest.main(JodaTimeTest.java:50)
This is the code:
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-ddTHH:mm:ssZ");
DateTime dt = formatter.parseDateTime(dstDateTime.toString());
You need to enclose the literal T within single quotes. Also the milliseconds are not properly patterned. You need to include SSS for the milliseconds. Have a look at the patterns here for more info.
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Update: To format the DateTime into a String representation of your choice, you need to do this.
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
DateTime dt = formatter.parseDateTime(dstDateTime.toString()); // You get a DateTime object
// Create a new formatter with the pattern you want
DateTimeFormatter formatter2 = DateTimeFormat.forPattern("dd-MMM-yy HH:mm:ss");
String dateStringInYourFormat = formatter2.print(dt); // format the DateTime to that pattern
System.out.println(dateStringInYourFormat); // Prints 16-Jun-14 12:30:00 because of the TimeZone I'm in
Either specify the timezone yourself or your default system timezone would be taken.
You need to change
"yyyy-MM-ddTHH:mm:ssZ"
To
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z"
Now
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z");
DateTime dt = formatter.parseDateTime("2014-06-16T07:00:00.000Z");
System.out.println(dt);
Output:
2014-06-16T07:00:00.000+05:30
Following code using Joda-Time library
Long timestamp = DateTime.parse(dateInString,DateTimeFormat.shortTime()).getMillis();
generates:
java.lang.IllegalArgumentException: Invalid format: "12.05.2014 11:42:35.808" is malformed at ".05.2014 11:42:35.808"
I tryed all DateTimeFormat.* but each format produces error.
How to fix it?
Build a DateTimeFormatter matching your pattern, and use that. Your pattern certainly isn't a "short time" pattern, given that you've got a date in there as well...
For example:
// Possibly MM.dd.yyyy - we don't know what 12.05.2014 is meant to represent
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd.MM.yyyy HH:mm:ss.SSS");
.withLocale(Locale.US)
.withZoneUTC(); // Adjust accordingly
DateTime dateTime = formatter.parse(text);
long millis = dateTime.getMillis();
try this
DateTimeFormatter pattern = DateTimeFormat.forPattern("dd.MM.yyy hh:mm:ss.SSS");
Long timestamp = DateTime.parse(dateInString,pattern).getMillis();
import java.util.*;
import java.text.*;
Date DateNow= new Date( );
SimpleDateFormat sdf =new SimpleDateFormat ("yyyy.MM.dd hh:mm:ss");
String timestamp= sdf.format(DateNow);
I am trying to parse the following string to date
2013-02-01T09:37:20EST
I need to compare it with current date to see it is before or after current date.
Here is what I am doing
Date formatTime(String time) throws exception
{
String format = "yyyy-MM-dd HH:mm:ss z";
DateFormat dateFormat = new SimpleDateFormat(format);
Date expTime = dateFormat.parse(time);
return expTime;
}
I am getting java.text.ParseException: Unparseable date: "2013-02-01T09:37:20EST" (at offset 10)
Thanks.
notice the space before the timezone z and wrap T around quotes 'T'
String format = "yyyy-MM-dd'T'HH:mm:ss z";
The format should be like this: yyyy-MM-dd'T'HH:mm:ssZ
Your input time String does not match your DateFormat pattern. You could use:
String format = "yyyy-MM-dd'T'HH:mm:ssz";
SimpleDateFormat