Android ISO8601: How to Convert a Date to call APIs

I have needed to get a date in java converted to ISO8601 on android for awhile and struggled through a few different issues on my way to a working solution.

Since it’s an app that I’m asking people to download, I don’t like to increase the download size by even a few MBs. So even though Joda Time is great, I need a different way. In researching, I had to piecemeal a solution from various blogs and stackoverflow posts.

If you’ve ever been wondering,
– Does android have a standard way to convert to iso8601 format?
– Why isn’t it easier to get a UTC date that follows iso8601 so I can just call the APIs?
Then hopefully I can save you some time.

Android ISO8601: UTC SimpleDateFormat

The following is a straightforward way to get a UTC ISO8601 string of a date object without using any external libraries.


SimpleDateFormat ISO8601DATETIMEFORMAT = new SimpleDateFormat("yyyy/MM/dd 'T'HH:mmZ");
TimeZone  tz = TimeZone.getTimeZone("UTC");
ISO8601DATETIMEFORMAT.setTimeZone(tz);
String isoDate = ISO8601DATETIMEFORMAT.format( dtobject);

Android ISO8601: APIs Don’t like Arabic Chars

But wait, sometimes this still sends over Arabic characters like:

\u0662\u0660\u0661\u0666-\u0660\u0662-\u0662\u0665T\u0660\u0664:\u0665\u0661Z 

We don’t want that.  The solution turns out to be fairly simple.  Make sure that the SimpleDateFormat is always converting using the English language locale.

SimpleDateFormat ISO8601DATETIMEFORMAT = new SimpleDateFormat("yyyy/MM/dd 'T'HH:mmZ",Locale.ENGLISH);

**Credit goes to this StackOverflow Comment Suggesting Locale.English

Android ISO8601: A Convenience Class

To wrap it all up, here’s a convenience method.  It can be dropped in without adding any major overhead to your app in terms of size or performance.


public class AndroidISO8601Util{

      static SimpleDateFormat ISO8601DATETIMEFORMAT = new SimpleDateFormat("yyyy/MM/dd 'T'HH:mmZ",Local.ENGLISH);

      //Note: this method is not thread safe because SimpleDateFormat is not thread safe
      public static String getISO8601forAPI( Date dt){
      TimeZone tz = TimeZone.getTimeZone("UTC");
      ISO8601DATETIMEFORMAT.setTimeZone(tz);
      return ISO8601DATETIMEFORMAT.format( dtobject);
    }
}

Export Unsigned APK with Android Studio

For years I developed with eclipse and used the Android Tools option to Export Unsigned APKs.

Eclipse Android Tools had an easy option to Export an Unsigned APK
Eclipse Android Tools had an easy option to Export an Unsigned APK

I could then take the unsigned .apk file and upload it to Amazon’s developer console.

This was one of the last items I was still using eclipse for after over a year of Android Studio development.  I finally spent some time figuring out how to make the same thing happen without needing eclipse any longer.  I found many of the questions and answers on stackoverflow to be confusing and inaccurate.  For example, many seem to think that using the debug apk is the same as using the unsigned release apk…which is not accurate.

This is the comment that ended up helping me get it right:  http://stackoverflow.com/a/32202274/966122

Here’s How to Export Unsigned APK in Android Studio

1. Edit the gradle build file for the project to specify an unsigned release signing.

buildTypes {
release {
minifyEnabled true
proguardFiles 'proguard.cfg'
}

releaseUnsigned.initWith(buildTypes.release)
releaseUnsigned {
signingConfig null
}

}

2. Generate Signed Apk using the releaseUnsigned Build Type.  This is on the 3rd or 4th screen in the flow of version 1.4 of Android Studio.

Release unsigned apk in android studio
Release unsigned apk in android studio

Enjoy the Unsigned APK

Once the build finishes, there will be an unsigned apk in the location you specified. You can then upload that apk to an emulator or to Amazon to start the process of letting amazon sign the app.