United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4341581 : RFE: time-stamp in ISO 8601 format

Details
Type:
Enhancement
Submit Date:
2000-05-26
Status:
Closed
Updated Date:
2013-10-02
Project Name:
JDK
Resolved Date:
2013-10-02
Component:
core-libs
OS:
generic
Sub-Component:
java.util:i18n
CPU:
generic
Priority:
P4
Resolution:
Won't Fix
Affected Versions:
1.3.0
Fixed Versions:

Related Reports

Sub Tasks

Description

Name: rlT66838			Date: 05/26/2000


All, Java 1.1.x, 1.2.x, 1.3

ISO 8601 defines the international date and time format which shall be used when
a local format is not appropriate. Some local conversions are already supported
by
  java.text.DateFormat
  java.text.SimpleDateFormat
But the ONE international format is not supported yet. It shall be in
  java.util.GregorianCalendar
Probably static conversion methods between long and String are the best
solution.

ISO 8601 example:
  1993-04-12T15:27:46-05:00
means 12 April 1993, 27 minutes and 46 seconds past 15 hours in the timezone 5
hours west of Greenwich. The time-zone filed is optional
(Review ID: 105392) 
======================================================================

                                    

Comments
java.time classes may be used to support the ISO 8601 formats.
                                     
2013-10-02
EVALUATION



Name: nl37777			Date: 12/09/2002

This is a reasonable request, especially since several other
J2SE subsystems already use ISO 8601 date formats. However, a few caveats:
- ISO 8601 doesn't specify a single date format, but a number of
possible formats. We need to pick one that makes sense in most
circumstances and specify clearly which one we picked.
- Support for ISO 8601 has also come up in the context of J2ME profiles
which don't necessarily have the Calendar classes. It may therefore be
better to add the new methods to the Date class.

======================================================================

Name: nl37777			Date: 07/07/2003

More information on ISO 8601 can be found at 
http://www.cl.cam.ac.uk/~mgk25/iso-time.html.


======================================================================


"Z" in SimpleDateFormat can be used to produce the zone value in "+hhmm" since 1.4. See Sample.java in Work Around.
###@###.### 2003-09-10
                                     
2003-09-10
WORK AROUND



Name: rlT66838			Date: 05/26/2000


public class SdaiCalendar extends java.util.GregorianCalendar {

	SdaiCalendar() {
		super();
	}


/** Converts time from string to long.
 * @return time as long variable.
 * @param time_stamp time written as string according to ISO 8601.
*/
	public long timeStampToLong(String time_stamp) {
		int sp;
		if (time_stamp.length() > 19) {
			sp = 1;
		} else {
			sp = 0;
		}
		int year = Integer.parseInt(time_stamp.substring(0, 4));
		int month = Integer.parseInt(time_stamp.substring(5, 7)) - 1;
		int day = Integer.parseInt(time_stamp.substring(8, 10));
		int hour = Integer.parseInt(time_stamp.substring(11 + sp, 13 +
sp));
		int minute = Integer.parseInt(time_stamp.substring(14 + sp, 16 +
sp));
		int second = Integer.parseInt(time_stamp.substring(17 + sp));
		set(year, month, day, hour, minute, second);
		return getTimeInMillis();
	}

/** Converts time from long to string.
 * @return time as string written according to ISO 8601.
 * @param time.
*/
	public String longToTimeStamp(long time) {
		setTimeInMillis(time);
		int year = get(YEAR);
		int month = get(MONTH) + 1;
		boolean month_one_digit;
		if (month < 10) {
			month_one_digit = true;
		} else {
			month_one_digit = false;
		}
		int day = get(DAY_OF_MONTH);
		boolean day_one_digit;
		if (day < 10) {
			day_one_digit = true;
		} else {
			day_one_digit = false;
		}
		int hour = get(HOUR_OF_DAY);
		boolean hour_one_digit;
		if (hour < 10) {
			hour_one_digit = true;
		} else {
			hour_one_digit = false;
		}
		int minute = get(MINUTE);
		boolean minute_one_digit;
		if (minute < 10) {
			minute_one_digit = true;
		} else {
			minute_one_digit = false;
		}
		int second = get(SECOND);
		boolean second_one_digit;
		if (second < 10) {
			second_one_digit = true;
		} else {
			second_one_digit = false;
		}
		String time_stamp = year + "-";
		if (month_one_digit) {
			time_stamp = time_stamp + "0";
		}
		time_stamp = time_stamp + month + "-";
		if (day_one_digit) {
			time_stamp = time_stamp + "0";
		}
		time_stamp = time_stamp + day + "T";
		if (hour_one_digit) {
			time_stamp = time_stamp + "0";
		}
		time_stamp = time_stamp + hour + ":";
		if (minute_one_digit) {
			time_stamp = time_stamp + "0";
		}
		time_stamp = time_stamp + minute + ":";
		if (second_one_digit) {
			time_stamp = time_stamp + "0";
		}
		return time_stamp + second;
	}

}
======================================================================


Since 1.4 SimpleDateFormat supports "Z" which produces the time zone value in +hhmm. The following is a sample program to produce a date string in the ISO 8601 style format.

import java.text.*;
import java.util.*;

public class Sample {
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    static Date now = new Date();

    public static void main(String[] args) {
        printDate(TimeZone.getTimeZone("UTC"));
        printDate(TimeZone.getTimeZone("America/Los_Angeles"));
        printDate(TimeZone.getTimeZone("Asia/Tokyo"));
    }

    static void printDate(TimeZone zone) {
        sdf.setTimeZone(zone);
        String date = sdf.format(now);
        date = date.replaceAll("\\+0000$", "Z");
        date = date.replaceAll("(\\d\\d)$", ":$1");  // if +hh:mm is preferred
        System.out.println(date);
    }
}
----------
Output:
$ java -showversion Sample
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)

2003-09-10T01:38:11.410Z
2003-09-09T18:38:11.410-07:00
2003-09-10T10:38:11.410+09:00

###@###.### 2003-09-10
                                     
2003-09-10



Hardware and Software, Engineered to Work Together