Why does parsing a date on some devices throw an ArrayIndexOutOfBoundsException? - date

I have the following string, representing a date and time, that's ISO 8601 compliant:
2014-03-11T11:57:15+0000
I'm using the Codename One SimpleDateFormat class to parse that string in to a Date object and then to populate a Calendar object:
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat(DateTimeFormats.ISO_8601);
Date date = format.parse("2014-03-11T11:57:15+0000");
calendar.setTime(date);
DateFormats.ISO_8601 is defined as
yyyy-MM-dd'T'HH:mm:ssZ
On the Codename One simulator this code correctly parses the string and sets date with the correct values. This also works on a Nokia C1-01. However, running the same code on a Nokia 206 or the DefaultCldcPhone1 emulator from the Java ME SDK results in an ArrayIndexOutOfBounds exception on the call to format.parse. The stack trace beyond that is obfuscated:
java.lang.ArrayIndexOutOfBoundsException
- java.util.Calendar.get(), bci=98
- al.a(), bci=3
- al.a(), bci=18
- al.parse(), bci=1013
Does anyone know why this is happening, how I can prevent it, or suggest a work-around for populating a Calendar from a string without having to write my own parser?
Any help much appreciated!

We recommend using the com.codename1.l10n.SimpleDateFormat class instead of the one from the java.text package. This will allow you to reproduce the issue on the simulator and debug it more accurately.

Related

How to get the Date with offset taken into account from XmlGregorianCalendar?

i have an XMLGregorianCalendar in this form: 2019-06-24T18:18:55.007+02:00
How can i get the right date (with offset taken into account) in String like this : 24/06/2019 16:18 ?
You tagged your question java-6. Yet I am presenting the modern answer using java.time, the modern Java date and time API. This is available in Java 6 through ThreeTen Backport (see the link at the bottom).
Depending on from where you got your XMLGregorianCalendar you may not need to have one. If I understood correctly, this class was used for representing dates and times in XML documents, where they are formatted like for example 2019-06-24T18:18:55.007+02:00. This format is close enough to ISO 8601 that the classes of java.time can parse it directly, which we prefer:
Locale userLocale = Locale.forLanguageTag("mt-MT"); // Maltese Malta
DateTimeFormatter displayFormatter
= DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
.withLocale(userLocale);
String fromXml = "2019-06-24T18:18:55.007+02:00";
OffsetDateTime dateTime = OffsetDateTime.parse(fromXml);
String formattedUtcDateTime = dateTime.withOffsetSameInstant(ZoneOffset.UTC)
.format(displayFormatter);
System.out.println(formattedUtcDateTime);
Output from this snippet is (tested in Java 1.7.0_67 with ThreeTen Backport version 1.3.6):
24/06/2019 16:18
Assuming that the formatting is for displaying to a user, I recommend using Java’s localized formats. I have chosen Maltese locale because it matches your requested format exactly. If your user is in a different locale, you should still consider the format of that locale.
Converting from XMLGregorianCalendar
If you got your XMLGregorianCalendar from a legacy API that you cannot afford to change just now, there are a number of options for converting it.
One is to get the string back from the XMLGregorianCalendar:
XMLGregorianCalendar xmlgc = DatatypeFactory.newInstance()
.newXMLGregorianCalendar("2019-06-24T18:18:55.007+02:00");
String fromXml = xmlgc.toString();
Now the rest is as above. Pro: it’s short, and I don’t think there are any surprises in it. Con: To me formatting back into the original string and parsing it once more feels like the detour.
Another option goes through the outdated GregorianCalendar class:
GregorianCalendar gc = xmlgc.toGregorianCalendar();
ZonedDateTime zdt = DateTimeUtils.toZonedDateTime(gc);
String formattedUtcDateTime = zdt.toOffsetDateTime()
.withOffsetSameInstant(ZoneOffset.UTC)
.format(displayFormatter);
Pro: I think it’s the official conversion. Con: it’s a line longer, and as I said, it uses the poorly designed and long outdated GregorianCalendar class.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Wikipedia article: ISO 8601
The working solution i have found:
private String getFormattedDate(final XMLGregorianCalendar xmlGregorianCalendar) {
Date time = xmlGregorianCalendar.toGregorianCalendar().getTime();
Date rightDate = DateUtils.addMinutes(time, - xmlGregorianCalendar.getTimezone());
String formatedDate = DateHelper.format(DateHelper.YYYYMMDDHHMMSS_DASH_COLONS, rightDate);
return formatedDate;}

How can I compare and check date of program and the system date in MVS 2012 Coded UI test?

I am trying to compare and check the date if it is today's date or not in a spesific program. I tried to use assertion method but when I use it the time will remain same if you try it next day. The main problem that I need to know when open a page from program It should be today's date and should be passed. if you know already anything about it please let me know also :)
Thanks yo!
Use System.DateTime.Now.ToString("yyyy-MM-dd") as one argument of the assertion. You may need to use a different format rather in the ...ToString() method. The exact format depends on how the date is shown on the screen.
This could be done using "StringAssert" to verify that your programs date string contains today's date string, while ignoring the time:
var programDateString = "7/25/2016 12:00:00"; //this is an example of your date retrieved from the application with time included
var todaysDate = System.DateTime.Today.ToShortDateString(); //short date string
StringAssert.Contains(programDateString, todaysDate);

migx TV (MODx REVO) - date format

I have a problem: I cannot bring date without time through MIGX TV
created by TV type migx:
{«Field»: «Date», «caption»: «Date», «inputTVtype»: «date»}
I deduce that everything appears normal by this way [[+ date]], but it is displayed as date and time and I want to display only the date part!
I tried this [[+ date: date ="% Y-% m-% d"]] but instead of the correct date 1/1/1970 is displayed.
I understand that the problem is that I use the TV type migx, but I need to use migx because there can be lots of dates!
Help me, please!
http://rtfm.modx.com/display/revolution20/Input+and+Output+Filters+%28Output+Modifiers%29 - you need :strtotime modificator before :date -
[[+mydate:strtotime:date=`%Y-%m-%d`]]

Why do I need to parse dates in Grails?

I am in the unfortunate position that I need to use a composite id in a Grails app where I work with legacy data. This means I have to override some actions in the controller, but as I did this I was struck by the fact that I could not use use a date argument directly as a parameter to a dynamic method.
Instead of just doing MyLegacyObj.findBySystemIdAndLogDate(params.systemId, params.logDate), I first needed to parse the date string before giving it to the dynamic method. To further complicate matters I had no idea what format the date string had (until I added lots of log.debug() string to the output). So now I have a bit of code looking like this
def formatter = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy")
MyLegacyObj.findBySystemIdAndLogDate(params.systemId, formatter.parse(params.logDate));
This feels unoptimal, no to say dangerous (what if the date format changes with the locale?)? What would be a recommended way of doing this, and do I really need to parse dates at all?
Date is a pretty complex object and params are just Strings, so Date is submitted in parts. It is "magically" assembled from the parts when assigning x.properties = params.
Command object will do the work for you, if you add a Date field to it.
It has nothing to do with methods' dynamic or static invocation. Your GSP that renders Date editor might interfere too.

Custom Date Formatting in J2ME

I would like to format a J2ME Date object to only show the date part, not the date and time. What would be the easiest way? Would probably need to include an external library to do this.
java.util.Calendar has all the methods required to format a date output.