I am trying to get "dd.MM.yy H:m" pattern of a date.
And I use the code given below
String dDate = "Fri Nov 10 22:50:46 EET 2017";
DateFormat dfr = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",Locale.ENGLISH);
Date cDate = dfr.parse(dDate);
DateFormat df = new SimpleDateFormat("dd.MM.yy H:m");
String result = df.format(cDate);
lastSyncDate.setText(getResources().getString(R.string.last_backup) + " " + result);
And I get
java.text.ParseException: Unparseable date: "Fri Nov 10 22:50:46 EET 2017" exception.
I cannot solve it and I think it is about DateFormat pattern.
Thank you for your solvings.
Related
i'm have string dates like Wed Aug 17 17:22:51 IST 2016 to parse to ISOdate. so I have tried following code to do that.
String tdate = "Wed Aug 17 17:22:51 IST 2016";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date pubDate = sdf.parse(tdate);
but it gave me:
java.text.ParseException: Unparseable date: "Wed Aug 17 17:22:51 IST 2016"
try parsing with
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
hope it helped
I'm getting this error:
java.text.ParseException: Unparseable date: "Fri Apr 08 2016 00:00:00 GMT+0530 (IST)"
I have used this SimpleDateFormat can any one suggest me a correct one?
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z (Z)");
If you are feeding date as "Fri Apr 08 2016 00:00:00 GMT+0530 (IST)". Then it's wrong. Please remove GMT. All time zones are calculated from GMT only.
Try passing date as "Fri Apr 08 2016 00:00:00 +0530 (IST)". It will work.
The correct parse-able date string should be:
Fri Apr 08 2016 00:00:00 IST (+0530)
This little snippet should clear the confusion. It's the reverse of what you're doing:
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z (Z)");
String strDate = format.format(new Date());
System.out.println(strDate);
Output is: Fri Apr 08 2016 17:26:34 IST (+0530)
You can try the pattern EEE MMM dd yyyy HH:mm:ss 'GMT'Z (z)
1) Using Java 1.6 :
System.out.println(fromStringToDate("Fri Apr 08 2016 00:00:00 GMT+0530 (IST)", "EEE MMM dd yyyy HH:mm:ss 'GMT'Z (z)"));
Output (in my system) : Fri Apr 08 00:00:00 IST 2016
Refer this link for timezone values Java TimeZone List
public static Date fromStringToDate(String myPotentialDate,String pattern) throws Exception{
// DateFormat myDateFormat = new SimpleDateFormat(pattern);
String countryCode = "US";
String languageCode = "en";
String timeZone = "Asia/Kolkata";
DateFormat myDateFormat = getDateFormat(pattern,countryCode,languageCode,timeZone);
// We set the Leniant to false
myDateFormat.setLenient(false);
try {
return myDateFormat.parse(myPotentialDate);
}
catch (ParseException e) {
// Unparsable date
throw new Exception("Unparsable date '"+myPotentialDate+"' with pattern '"+pattern+"'. Due to '"+e+"'",e);
}
}
private static DateFormat getDateFormat(String pattern,String countryCode,String languageCode,String timeZoneId){
// We build the Local
Locale myLocale = new Locale(languageCode,countryCode);
// We build the DateFormat with the Local and the pattern
DateFormat myDateFormat = new SimpleDateFormat(pattern,myLocale);
// We set the TimeZone to the correct one
myDateFormat.setTimeZone(TimeZone.getTimeZone(timeZoneId));
// We set the Leniant to false
myDateFormat.setLenient(false);
return myDateFormat;
}
2)Using Java 1.8 Java8 Date time API
String countryCode = "US";
String languageCode = "en";
String timeZoneId = "Asia/Kolkata";
LocalDateTime dt = LocalDateTime.parse("Fri Apr 08 2016 00:00:00 GMT+0530 (IST)",
DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss 'GMT'Z (z)").withLocale(new Locale(languageCode,countryCode)));
ZoneId zoneId= ZoneId.of(timeZoneId);
ZonedDateTime zdt= ZonedDateTime.of(dt, zoneId);
System.out.println(zdt);
Output:
2016-04-08T00:00+05:30[Asia/Kolkata]
This question already has answers here:
How to get a Date object from String
(5 answers)
Closed 8 years ago.
So I am using DateFormat to convert a date (that I parsed form a CSV) to YYYY-MM-DD format, so I can INSERT it into a MYSQL column which is in DATE format.
I am using DateFormat, but my date output keeps coming out like this:
Mon Dec 29 00:00:00 CST 2014
Tue Dec 30 00:00:00 CST 2014
Tue Dec 30 00:00:00 CST 2014
Tue Dec 30 00:00:00 CST 2014
Wed Dec 31 00:00:00 CST 2014
How can I convert this date to the format I am looking for?
DateFormat formatter;
Date date;
formatter = new SimpleDateFormat("yyyy-MM-dd");
date = formatter.parse(date_parsed[0].toString());
System.out.println("DATE=" + date);
Try this:
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
String dateStr = "Tue Dec 30 00:00:00 CST 2014";
Date date = formatter.parse( dateStr );
formatter.applyPattern("yyyy-MM-dd");
String newDate = formatter.format( date );
System.out.println("DATE=" + newDate);
Output :
DATE=2014-12-30
I am using the below code to parse a date, and to format it how I want; the code is working fine for other dates such as this one:
Thu, 04 Sep 2014 19:32:00 GMT
But it is throwing the above error message on a date such as this:
Fri, 05 Sep 2014 06:01:00 +0100
Can anyone explain why?
Thanks.
The code:
//Only get the 'important' parts.
pubDateFormat = pubDate.substring(0, 23);
//Format it how I want.
dateFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm");
//Here is where it is breaking.
Date date = dateFormatter.parse(pubDateFormat);
//Format it once again to how I want.
dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
//get the current date and format it the same.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
currDate = sdf.format(new Date());
currDate = currDate.replace(" ", "");
rssDate = dateFormatter.format(date).replace(" ", "");
This is the log:
09-05 11:30:07.153: E/DATE(7665): Fri, 05 Sep 2014 06:01
09-05 11:30:07.153: E/ERROR(7665): java.text.ParseException: Unparseable date: "
09-05 11:30:07.153: E/ERROR(7665): Fri, 05 Sep 2014 06:01" (at offset 0)
You have to add the locale:
SimpleDateFormat dateFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm", Locale.ENGLISH);
Try this
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm", Locale.US);
This is the String I'm extracting the info from using a regex:
2823893a2f91c7507831f140dd7aa75e420477b0 - #0023922: Fixed the message
for defaulted bonds ; Thu Oct 25 12:08:25 2012 +0000
This is the code I use to extract the String and then try to make it into a Date:
Pattern pattern3 = Pattern.compile(";\\s(.*)");
Matcher matcher3 = pattern3.matcher(s);
matcher3.find();
String t = matcher3.group(1).toString();
try {
Date time = new SimpleDateFormat("dd/MMM/yy hh:mm a").parse(t);
} catch (ParseException e) {
e.printStackTrace();
}
This should be the format of my input:
Thu Oct 25 12:08:25 2012 +0000
And what I want is to make a Date from the aforementioned string which looks like:
25/Oct/12 12:08 PM
But I keep getting these errors:
java.text.ParseException: Unparseable date: "Thu Oct 25 12:08:25 2012 +0000"
Fixed the message for defaulted bonds0null
at java.text.DateFormat.parse(DateFormat.java:337)
at GitItem.cultivateGitItem(GitItem.java:42)
at main.main(main.java:9)
java.text.ParseException: Unparseable date: "Thu Oct 25 11:52:39 2012 +0000"
at java.text.DateFormat.parse(DateFormat.java:337)
at GitItem.cultivateGitItem(GitItem.java:42)
at main.main(main.java:9)
Your pattern has to match the pattern of the incoming data, which it doesn't right now.
SimpleDataFormat can't read your mind, the pattern you are giving it doesn't match the format you are passing into .parse().
"dd/MMM/yy hh:mm a" will never match Thu Oct 25 12:08:25 2012 +0000, you have to specify the exact pattern that the incoming data is in, this is very well documented in the JavaDocs.
Then you can change the pattern to what you want using .applyPattern() can call .format() to get the formatted output you want.
I would simply remove the unwanted part:
String dateAsString = s.replaceAll(".*;\\s+","");
Then you need to DateFormat: one to parse the string and another one to output the correct format:
String s = "2823893a2f91c7507831f140dd7aa75e420477b0 - #0023922: Fixed the message for defaulted bonds ; Thu Oct 25 12:08:25 2012 +0000";
System.out.println("s = " + s);
String dateAsString = s.replaceAll(".*;\\s+","");
System.out.println("dateAsString = " + dateAsString);
DateFormat parser = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy X", Locale.ENGLISH);
Date date = parser.parse(dateAsString);
System.out.println("date = " + date);
DateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy hh:mm a", Locale.ENGLISH);
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(formatter.format(date));
outputs:
s = 2823893a2f91c7507831f140dd7aa75e420477b0 - #0023922: Fixed the message for defaulted bonds ; Thu Oct 25 12:08:25 2012 +0000
dateAsString = Thu Oct 25 12:08:25 2012 +0000
date = Thu Oct 25 14:08:25 CEST 2012
25/Oct/2012 12:08 PM
Note: you need to use the appropriate locale to parse and print the month/day names