package test;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;
import org.joda.time.format.DateTimeParser;
import org.joda.time.format.ISODateTimeFormat;

/**
 * Test class.
 *
 * @author John Jenkins
 */
public class Test {
	/**
	 * The date-time formatter for the W3C's specifications for date-time 
	 * values.
	 */
	private static final DateTimeFormatter ISO_W3C_DATE_TIME_FORMATTER;
	static {
		// The W3C define 6 formats as valid ISO date/time values.
		DateTimeParser[] parsers = new DateTimeParser[6];
		
		// Just the year.
		parsers[0] = ISODateTimeFormat.year().withZoneUTC().getParser();
		
		// The year and month.
		parsers[1] = ISODateTimeFormat.yearMonth().withZoneUTC().getParser();
		
		// The year, month, and day.
		parsers[2] = 
			ISODateTimeFormat.yearMonthDay().withZoneUTC().getParser();
		
		// The year, month, day, hour, minute, and time zone.
		// Build the parser from the existing ISODateTimeParser for the year,
		// month, day, hour, and minute and add the time zone.
		DateTimeFormatterBuilder dateHourMinuteTimezone =
			new DateTimeFormatterBuilder();
		dateHourMinuteTimezone.append(ISODateTimeFormat.dateHourMinute());
		dateHourMinuteTimezone.append(DateTimeFormat.forPattern("ZZ"));
		parsers[3] = dateHourMinuteTimezone.toParser();
		
		// The year, month, day, hour, minute, second, and time zone.
		parsers[4] = ISODateTimeFormat.dateTimeNoMillis().getParser();
		
		// The year, month, day, hour, minute, and time zone.
		// Build the parser from the existing ISODateTimeParser for the year,
		// month, day, hour, and minute and add the time zone.
		parsers[5] = ISODateTimeFormat.dateTime().getParser();
		
		// Build the formatter with the 6 parsers and the complete ISO 
		// date-time printer.
		DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
		builder.append(ISODateTimeFormat.dateTime().getPrinter(), parsers);
		ISO_W3C_DATE_TIME_FORMATTER = builder.toFormatter();
		
		ISO_W3C_DATE_TIME_FORMATTER.withZoneUTC();
	}
	
	/**
	 * Decodes two strings and compares their value. They represent the same
	 * point in time if UTC is the default, but the "noTime" is being decoded
	 * and assigned a time zone and then is being adjusted for the new time
	 * zone.
	 * 
	 * @param args Ignored.
	 * 
	 * @throws Exception Not thrown.
	 */
	public static void main(String args[]) throws Exception {
		DateTime noTime = 
			ISO_W3C_DATE_TIME_FORMATTER.parseDateTime("2012-08-15");
		DateTime withTime = 
			ISO_W3C_DATE_TIME_FORMATTER
				.parseDateTime("2012-08-15T00:00:00.000Z");
		
		if(noTime.isEqual(withTime)) {
			System.out.println("Equal.");
		}
		else {
			System.out.println("Not equal.");
		}
	}
}