0

I am receiving iso format date in Java. How can I parse it to get MM and DD.

Date in iso format:2015-08-10T21:00:00.090Z

Andy Turner
  • 137,514
  • 11
  • 162
  • 243

2 Answers2

2

java.time

The other answer uses troublesome old date-time classes now supplanted by the java.time classes.

The java.time classes use ISO 8601 formats by default when parsing and generating Strings that represent date-time values.

An Instant represents a moment on the timeline in UTC with a resolution of nanoseconds.

Instant instant = Instant.parse( "2015-08-10T21:00:00.090Z" );

Adjust into the time zone through which you want to see this moment.

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );

You can interrogate for month and day-of-month.

int month = zdt.getMonthValue();
int dayOfMonth = zdt.getDayOfMonth();
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

Some references

http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html

Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault()) ;
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
try {
        Date date = dateformat.parse("2015-08-10T21:00:00.090Z");
        cal.setTime(date);
        String result = new SimpleDateFormat("MM dd", Locale.getDefault()).format(cal.getTime());
        System.out.println(result);

} catch (ParseException e) {
        e.printStackTrace();
}   

Hope it helps

Daniel Rosano
  • 695
  • 5
  • 16