package com.levelup.java.date.format;

import static org.junit.Assert.assertEquals;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.junit.Test;

/**
 * This java example will demonstrate formatting a date of January 15, 1967.
 * 
 * @author Justin Musgrove
 * @see <a href='http://www.leveluplunch.com/java/examples/format-date/'></a>
 * 
 */
public class FormatDate {
	
	@Test
	public void format_date_in_java () {
		
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.MONTH, Calendar.JANUARY);
		cal.set(Calendar.DAY_OF_MONTH, 15);
		cal.set(Calendar.YEAR, 1967);

		// http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
		SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
		String dateFirstSuperBowlWasPlayed = dateFormatter.format(cal.getTime());
		
		// dateFirstSuperBowlWasPlayed = 01/15/1967
		assertEquals("01/15/1967", dateFirstSuperBowlWasPlayed);
		
	}
	
	@Test
	public void format_date_in_java_using_printf() {

		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.MONTH, Calendar.JANUARY);
		cal.set(Calendar.DAY_OF_MONTH, 15);
		cal.set(Calendar.YEAR, 1967);
		cal.set(Calendar.HOUR, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);

		// display time and date using toString()
		String str = String.format("Current Date/Time : %tc", cal.getTime());
		
		assertEquals("Current Date/Time : Sun Jan 15 12:00:00 CST 1967", str);
	}
	
	
	@Test
	public void format_date_in_java_with_joda () {
		
		DateTime dateFirstSuperBowlWasPlayed = new DateTime(1967, 1, 15, 0, 0);

		DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd/yyyy");
		
		assertEquals("01/15/1967", dateFirstSuperBowlWasPlayed.toString(fmt));

	}
	
	@Test
	public void format_date_in_java_with_apachecommons () {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.MONTH, Calendar.JANUARY);
		cal.set(Calendar.DAY_OF_MONTH, 15);
		cal.set(Calendar.YEAR, 1967);

		String dateFirstSuperBowlWasPlayed = DateFormatUtils.format(cal, "MM/dd/yyyy");
		assertEquals("01/15/1967", dateFirstSuperBowlWasPlayed);
	}
	
	

}

@@ -530,13 +530,24 @@
 e/'%3E
+Format date
 %3C/a%3E%0A
-
  * %0A
@@ -787,86 +787,8 @@
 );%0A%0A
-%09%09// http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html%0A
 %09%09Si
@@ -937,54 +937,8 @@
 %0A%09%09%0A
-%09%09// dateFirstSuperBowlWasPlayed = 01/15/1967%0A
 %09%09as
@@ -992,19 +992,16 @@
 layed);%0A
-%09%09%0A
 %09%7D%0A%09%0A%09@T
@@ -1047,14 +1047,21 @@
 ing_
-p
+st
 rin
+g_forma
 t
-f
 () %7B
@@ -1350,52 +1350,8 @@
 );%0A%0A
-%09%09// display time and date using toString()%0A
 %09%09St
@@ -1700,24 +1700,24 @@
 /yyyy%22);%0A%09%09%0A
+
 %09%09assertEqua
@@ -1777,17 +1777,16 @@
 (fmt));%0A
-%0A
 %09%7D%0A%09%0A%09@T

format date added url, etc