Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.messners.gitlab.api;
  
  import java.util.Calendar;
  import java.util.Date;
  import java.util.TimeZone;

This class provides utility methods for parsing and formatting ISO8601 formatted dates.

Author(s):
Greg Messner <greg@messners.com>
 
 public class ISO8601 {
 	public static final String PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ";
 	public static final String OUTPUT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss'Z'";
 	public static final String ALTERNATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
 
 	private static final SimpleDateFormat iso8601Format;
 	private static final SimpleDateFormat iso8601OutputFormat;
 	private static final SimpleDateFormat iso8601AlternateFormat;
 	static { 
 		.setTimeZone(TimeZone.getTimeZone("GMT"));  
 	}


Get a ISO8601 formatted string for the current date and time.

Returns:
a ISO8601 formatted string for the current date and time
 
 	public static String getTimestamp () {
 		return .format(new Date());
 	}


Get a ISO8601 formatted string for the provided Calendar instance.

Parameters:
cal the Calendar instance to get the ISO8601 formatted string for
Returns:
a ISO8601 formatted string for the provided Calendar instance, or null if call is null
 
 	public static String toString (Calendar cal) {
 		
 		if (cal == null) {
 			return (null);
 		}
 
 		return toString(cal.getTime());
 	}


Get a ISO8601 formatted string for the provided Date instance.

Parameters:
date the Date instance to get the ISO8601 formatted string for
Returns:
a ISO8601 formatted string for the provided Date instance, or null if date is null
 
 	public static synchronized String toString (Date date) {
 		
 		if (date == null) {
 			return (null);
 		}
 		
 		return .format(date);
 	}


Parses an ISO8601 formatted string a returns a Date instance.

Parameters:
dateTimeString the ISO8601 formatted string
Returns:
a Date instance for the ISO8601 formatted string
Throws:
java.text.ParseException if the provided string is not in the proper format
 
 	public static Date toDate (String dateTimeStringthrows ParseException {
 		
 		if (dateTimeString == null) {
 			return (null);
 		}
 		
 		dateTimeString = dateTimeString.trim();
 
 		if (dateTimeString.length() > 10) {
 			fmt = (dateTimeString.charAt(10) == 'T' ? 
 		} else {
 			fmt = ;
 		}
 
		synchronized (fmt) {
			return (fmt.parse(dateTimeString));
		}		
	}


Parses an ISO8601 formatted string a returns a Calendar instance.

Parameters:
dateTimeString the ISO8601 formatted string
Returns:
a Calendar instance for the ISO8601 formatted string
Throws:
java.text.ParseException if the provided string is not in the proper format
	public static Calendar toCalendar (String dateTimeStringthrows ParseException {
		Date date = toDate(dateTimeString);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return (cal);
	}
New to GrepCode? Check out our FAQ X