I am getting an Illegal Argument Exception from the Joda time library but only in simplified Chinese.
java.lang.IllegalArgumentException: Invalid format: "10月 01, 2015 10:25 PM"
at org.joda.time.format.DateTimeFormatter.parseDateTime(Unknown Source)
My knowledge of Chinese is zero so I am not sure why it is failing.
You may try like this:
Locale locale = Locale.SIMPLIFIED_CHINESE;
SimpleDateFormat myFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss a",locale);
SimpleDateFormat fromUser = new SimpleDateFormat("dd MMM yyyy HH:mm:ss a",Locale.US);
try {
date = myFormat.format(fromUser.parse(date));
} catch (Exception e) {
// Log.d("tttt","Ex prob ar >"+e.getMessage());
}
Related
I am currently working on a chat application by using Firestore. When I try to get Timestamp from firestore Document with
Date date=documentSnapshot.getDate(fieldvalue1,DocumentSnapshot.ServerTimestampBehavior.ESTIMATE);
then it returns to date in "Mon Sep 17 05:52:14 EDT 2018" this format.I am not able to convert that date into Simple date format. Please help.
You can use EEE MMM dd HH:mm:ss z yyyy pattern for retrieve date from string. Like,
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
try {
Date date2 = format.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
EDIT
You can convert into Indian time Like,
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
try {
format.setTimeZone(TimeZone.getTimeZone("GMT-04:00")); //for EDT
Date date2 = format.parse(dateStr);
TimeZone tz = TimeZone.getTimeZone("GMT+5:30"); //for Indian Time
format.setTimeZone(tz);
String result = format.format(date2);
} catch (ParseException e) {
e.printStackTrace();
}
I'm working on an Application that supports multi-language (English and Spanish). I'm stuck with a simple date format conversation which is only failing on Android 7.0 and above when Locale = es.
This is strange because the same code is working absolutely fine on 6.0 and prior SDKs version. I also checked https://developer.android.com/guide/topics/resources/multilingual-support.html
and it wasn't much of a help.
This is the code
public String getDateTimeInDeviceLocale(Context context, String time) {
String output = "";/*Tue, 27 Aug 2013 18:10:00 Z*/
SimpleDateFormat utcFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'Z'", Locale.getDefault());
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
if (null != time) {
Date date = new Date();
date = utcFormat.parse(time); //Throws exception only on Espanol
SimpleDateFormat format;
if (is24HourFormat(context)) {
format = new SimpleDateFormat("EEE MMM dd, yyyy HH:mm", Locale.getDefault());
}else {
format = new SimpleDateFormat("EEE MMM dd, yyyy hh:mm aa", Locale.getDefault());
}
output = format.format(date);
String timeZoneName = TimeZone.getDefault().getDisplayName(
TimeZone.getDefault().inDaylightTime(new Date()),
TimeZone.SHORT);
if (!TextUtils.isEmpty(timeZoneName)) {
output = output + " " + timeZoneName;
}
}
} catch (ParseException e) {
// Unparseable date
}
Thanks for your help.
I tested it and seems fine to me, check other areas of your application
#Test
public void testGetDatetimeInDeviceLocale_en() throws ParseException{
Locale english = Locale.ENGLISH;
SimpleDateFormat utcFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'Z'", english);
String testableDate = utcFormat.format(new Date(0));
// test 24 hours format
assertEquals("Thu Jan 01, 1970 02:00 BST", getDateTimeInDeviceLocale(true, testableDate, english));
// test AM PM format
assertEquals("Thu Jan 01, 1970 02:00 AM BST", getDateTimeInDeviceLocale(false, testableDate, english));
}
#Test
public void testGetDatetimeInDeviceLocale_es() throws ParseException{
Locale spanish = Locale.forLanguageTag("es");
SimpleDateFormat utcFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'Z'", spanish);
String testableDate = utcFormat.format(new Date(0));
// test 24 hours format
assertEquals("jue ene 01, 1970 02:00 BST", getDateTimeInDeviceLocale(true, testableDate, spanish));
// test AM PM format
assertEquals("jue ene 01, 1970 02:00 AM BST", getDateTimeInDeviceLocale(false, testableDate, spanish));
}
P.S. As I suggested in the comment above you should change your method signature to the following:
public String getDateTimeInDeviceLocale(boolean is24Hours, String time, Locale locale) throws ParseException
P.P.S I have removed your catch and let the exception to be thrown to get error feedback.
I am trying to convert the following data time to EEE, d MMM yyyy format but kept getting Unparsable date format. Can some one please help. I couldn't find anything on the web that helped.
Here is the code
String datestr = "2017-01-12T00:00:00Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
Date convertedDateStart = new Date();
try {
convertedDateStart = dateFormat.parse(datestr);
camp_new.startdate = convertedDateStart;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Any help wound be greatly appreciated. Thanks in advance
The date string "2017-01-12T00:00:00Z" cannot be parsed with your format "EEE, d MMM yyyy HH:mm:ss Z"; the fields are the wrong type or in the wrong place.
The format for your datestr "2017-01-12T00:00:00Z" should be:
yyyy-MM-dd'T'HH:mm:ssZ
You need to initialize the SimpleDateFormat with this format to parse datestr.
The format "EEE, d MMM yyyy HH:mm:ss Z" would parse a date that looks like
Thu, 1 Jan 2017 00:00:00 Z
Read the docs to understand the difference between parse() and format().
You have to parse it first, store it as a Date object and then use another SimpleDateFormat to format in the desired way.
String datestr = "2017-01-12T00:00:00Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US);
SimpleDateFormat dateFormat2 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
Date date = dateFormat.parse(datestr);
String convertedDateStart = dateFormat2.format(date);
Well This will Work For you if you want to FORMAT and get a Date String with that format..
String datestr = "2017-01-12T00:00:00Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
try {
date = dateFormat.parse(datestr);
} catch (ParseException e) {
e.printStackTrace();
}
dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
datestr = dateFormat.format(date);
System.out.println(datestr);
This will give you Thu, 12 Jan 2017 00:00:00 +0430 Date String.
I am working on Android application in which I am going to send my date to the server. But I am getting the error of "Unparsable date exception", my date is: "3/12/2018".
My code is given below:
SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",Locale.ENGLISH);
Date parsedDate = sdf.parse(setDateTxt); //setDateTxt returns 3/12/2018 from date picker
SimpleDateFormat print = new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
object.put("dateOfBirth", print.format(parsedDate));
the SimpleDateFormat has to match your setDateText pattern.
SimpleDateFormat sdf = new SimpleDateFormat("M/d/yyyy",Locale.ENGLISH);
I'm having trouble parsing a date that I'm trying to reformat. The SimpleDateFormat is giving me a real headache.
I'm getting this date from a news feed:
Wed, 06 Mar 2013 09:22:00 +0100
And I need to format it to this:
06.03.2013
I could just use a hashmap with all the months but I would like to use the SimpleDateFormat, since that's what it's for.
But I can't seem to find the right pattern.
Try adding following code snippet:--
//SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss +SSSS");
SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss +SSSS", Locale.ENGLISH);
Date date = null;
try {
date = fmt.parse("Wed, 06 Mar 2013 09:22:00 +0100");//Hardcoded for your case...
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat fmtOut = new SimpleDateFormat("dd.MM.yyyy");
System.out.println("Date :-- " +fmtOut.format(date));