SimpleDateFormat not working on 7.0 and above - android

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.

Related

Date parsing issue in android

I am trying to parse a date in android using the following code
String dateString = "Sun Apr 15 13:37:33 CEST 2012";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
System.out.println(sdf.parse(dateString));
The code above works fine with java, but in Android it gives ParseException.
java.text.ParseException: Unparseable date: "Sun Apr 15 13:37:33 CEST 2012" (at offset 20)
What could be the possible reason?
I am running it on Samsung S4, lollipop, system language is French, and system timezone is GMT+2:00.
try this code:
String mytime="Jan 17, 2012";
SimpleDateFormat dateFormat = new SimpleDateFormat(
"MMM dd, yyyy");
Date myDate = null;
try {
myDate = dateFormat.parse(mytime);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat timeFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss
zzz yyyy");
String finalDate = timeFormat.format(myDate);
TextView t = (TextView) findViewById(R.id.txt);
t.setText(finalDate);

SimpleDateFormat throws exception on a real phone but works correctly on an emulator [duplicate]

This question already has an answer here:
SimpleDateFormat works correctly on emulator and is wrong on the device
(1 answer)
Closed 6 years ago.
Trying to format date and put it into TextView in Android Studio from
Mon, 29 Feb 2016 22:31:00 GMT
to
2016-02-29 22:31:00
In my Genymotion Google Nexus 5, Samsung Galaxy S5, etc devices it looks perfect. When i run this application on a real phone(not just one) TextView with date was empty: getFormatPubDate throws ParseException with "Unparseable date" on
date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z").parse(pubDate);
Date in pubDate variable is correct and not null.
// strings.xml
<string name="data_time_format">yyyy-MM-dd HH:mm:ss</string>
// someClass.java
public String getFormatPubDate(String format){
try {
Date date = null;
date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z").parse(pubDate);
return new SimpleDateFormat(format).format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return pubDate;
}
// someActivity.java
viewHolder.timePublishedTextView.setText(cachedNews.getFormatPubDate(activity.getResources().getString(R.string.data_time_format)));
P.S. I use format with HH (24 hour) but time is not correct. Example
from emulator - Mon, 29 Feb 2016 23:01:00 GMT formated to 2016-02-29
18:01:00
//This is original date format
String date = "Mon, 29 Feb 2016 22:31:00";
SimpleDateFormat sdf = new SimpleDateFormat("EE, dd MMM yyyy HH:mm:ss",
Locale.ENGLISH);
//This is new date format
SimpleDateFormat print = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parsedDate = null;
try
{
parsedDate = sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
String text = print.format(parsedDate);

Parsing date using SimpleDateFormat to get Time

I am working on an Android application in which I need to parse my date to get my time. I am getting parsing exception when I am using my below code.
Here is the code:
SimpleDateFormat parseFormat = new SimpleDateFormat("dd MM hh:mm:ss yyyy");
SimpleDateFormat printFormat = new SimpleDateFormat("h:mm:ss");
Date date;
try { //selectedDateTime = Fri Jan 15 09:30:44 GMT+04:00 2016
date = parseFormat.parse(selectedDateTime+"");
System.out.println(printFormat.format(date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Here is the exception:
java.text.ParseException: Unparseable date: "Fri Jan 15 09:30:00 GMT+04:00 2016" (at offset 0)
SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Your formatting is off, try this.
Your Strings are wrong. The correct ones are:
SimpleDateFormat parseFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
SimpleDateFormat printFormat = new SimpleDateFormat("HH:mm:ss");
I guess your initial date format is wrong. Can you share the value of selectedDateTime.
I guess it in different format (as per your comment) like Fri Jan 15 09:30:44 GMT+04:00 2016
If so please try this code
SimpleDateFormat parseFormat = new SimpleDateFormat("E MM dd hh:mm:ss z yyyy");
SimpleDateFormat printFormat = new SimpleDateFormat("h:mm:ss");
Date date;
try { //selectedDateTime = Fri Jan 15 09:30:44 GMT+04:00 2016
date = parseFormat.parse(selectedDateTime+"");
System.out.println(printFormat.format(date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
update
try {
String dateTest = "Fri Jan 15 09:30:00 GMT+04:00 2016";
SimpleDateFormat formatter = new SimpleDateFormat(
"EEE MMM dd hh:mm:ss zzz yyyy");
java.util.Date d = (java.util.Date) formatter.parse(dateTest);
System.out.println("" + d);
SimpleDateFormat print = new SimpleDateFormat("hh:mm:ss");
System.out.println(print.format(d));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
output
Fri Jan 15 11:00:00 IST 2016
11:00:00
Following code can help you understand how exactly formatting syntax change for date formatting
Calendar calendar = Calendar.getInstance();
System.out.println("Date: \t"+calendar.getTimeInMillis());
String dateAsText_ = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(Long.parseLong(""+calendar.getTimeInMillis())));
Date date = new Date(Long.parseLong(""+calendar.getTimeInMillis()));
System.out.println(""+dateAsText_);
SimpleDateFormat ft = new SimpleDateFormat ("dd-MMM-yyyy");
System.out.println(""+ft.format(date));
SimpleDateFormat time_ft = new SimpleDateFormat ("hh:mm aa");
System.out.println(""+time_ft.format(date));
Output
Date: 1452766759531
2016-01-14 15:49:19
14-Jan-2016
03:49 PM

Invalid Date format for Simplified Chinese

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

Unable to get date in the appropriate format in android

I am obtaining the Date and converting into the GMT format. I am obtaining it in the form of Thu Jul 24 06:55:56 GMT+05:30 2014. I want the date to be displayed in the following form 6/19/2014 12:28:44 PM. Can anyone tell me step by step how to do it. I read the following document http://developer.android.com/reference/java/text/SimpleDateFormat.html but the format tends to remain the same even if I use a and L. I am posting the code below, please guide me.
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("LL/dd/yyyy HH:mm:ss a");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("LL/dd/yyyy HH:mm:ss a");
//Time in GMT
try {
dateFormatLocal.parse( dateFormatGmt.format(new Date()) );
Log.i("gmt time",""+dateFormatLocal.parse( dateFormatGmt.format(new Date()) ));
date_edittext.setText(""+dateFormatLocal.parse( dateFormatGmt.format(new Date()) ));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
use this :
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.getDefault());
say if it works :)
SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(Date.parse("Your date string"));
String strCurrentDate = "Wed, 18 Apr 2012 07:55:29 +0000";
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss Z");
String currentDate = format.format(strCurrentDate);
format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(Date.parse(currentDate));
I don't quite get your problem but look :
Date test = new Date();
test.setHours(10);
SimpleDateFormat sdf = new SimpleDateFormat("M/dd/yyyy HH:mm:ss a");
System.out.println(sdf.format(test));
This displays 7/24/2014 10:13:01 AM
If I change setHours to test.setHours(22) this displays 7/24/2014 10:13:01 PM
Hope this helps.

Categories

Resources