All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hirondelle.date4j.Examples Maven / Gradle / Ivy

package hirondelle.date4j;

import java.util.Locale;
import java.util.TimeZone;

/** Examples of how to use date4j. */
public final class Examples {

  /** 
   Run the examples, and output to the console. 
   
  

Example output when you run this class:

   Current date-time in default time zone : 2011-10-24 08:05:59
   Current date-time in Cairo : 2011-10-24 13:06:00 (Monday)
   Age of someone born May 16, 1995 is : 16
   The 3rd Friday of this month is : 2011-10-21
   Number of days till Christmas : 62
   90 days from today is : 2012-01-22
   3 months and 5 days from today is : 2012-01-29
   Numbers of hours difference between Paris and Perth : 6
   The number of weeks since Sep 6, 2010 : 59
   This many seconds till midnight : 57240
   Output using an ISO format: 2011-10-24T08:06:00
   The first day of this week is : 2011-10-23
   The number of years the JDK date-time API has been suctorial : 15
  
*/ public static void main(String... aArgs){ Examples examples = new Examples(); examples.currentDateTime(); examples.currentDateTimeInCairo(); examples.ageIfBornOnCertainDate(); examples.optionsExpiry(); examples.daysTillChristmas(); examples.whenIs90DaysFromToday(); examples.whenIs3Months5DaysFromToday(); examples.hoursDifferenceBetweenParisAndPerth(); examples.weeksSinceStart(); examples.timeTillMidnight(); examples.imitateISOFormat(); examples.firstDayOfThisWeek(); examples.jdkDatesSuctorial(); } // PRIVATE private static void log(Object aMsg){ System.out.println(String.valueOf(aMsg)); } /** What is the current date-time in the JRE's default time zone? */ private void currentDateTime(){ DateTime now = DateTime.now(TimeZone.getDefault()); String result = now.format("YYYY-MM-DD hh:mm:ss"); log("Current date-time in default time zone : " + result); } /** What is the current date-time in Cairo (include weekday)? */ private void currentDateTimeInCairo(){ DateTime now = DateTime.now(TimeZone.getTimeZone("Africa/Cairo")); String result = now.format("YYYY-MM-DD hh:mm:ss (WWWW)", Locale.getDefault()); log("Current date-time in Cairo : " + result); } /** What's the age of someone born May 16, 1995? */ private void ageIfBornOnCertainDate(){ DateTime today = DateTime.today(TimeZone.getDefault()); DateTime birthdate = DateTime.forDateOnly(1995, 5, 16); int age = today.getYear() - birthdate.getYear(); if(today.getDayOfYear() < birthdate.getDayOfYear()){ age = age - 1; } log("Age of someone born May 16, 1995 is : " + age); } /** Stock options expire on the 3rd Friday of this month. What day of the month is that? */ private void optionsExpiry(){ DateTime today = DateTime.today(TimeZone.getDefault()); DateTime firstOfMonth = today.getStartOfMonth(); int result = 0; if (firstOfMonth.getWeekDay() == 7){ result = 21; } else { result = 21 - firstOfMonth.getWeekDay(); } DateTime thirdFriday = DateTime.forDateOnly(firstOfMonth.getYear(), firstOfMonth.getMonth(), result); log("The 3rd Friday of this month is : " + thirdFriday.format("YYYY-MM-DD")); } /** How many days till the next December 25? */ private void daysTillChristmas(){ DateTime today = DateTime.today(TimeZone.getDefault()); DateTime christmas = DateTime.forDateOnly(today.getYear(), 12, 25); int result = 0; if(today.isSameDayAs(christmas)){ // do nothing } else if (today.lt(christmas)){ result = today.numDaysFrom(christmas); } else if (today.gt(christmas)){ DateTime christmasNextYear = DateTime.forDateOnly(today.getYear() + 1, 12, 25); result = today.numDaysFrom(christmasNextYear); } log("Number of days till Christmas : " + result); } /** What day is 90 days from today? */ private void whenIs90DaysFromToday(){ DateTime today = DateTime.today(TimeZone.getDefault()); log("90 days from today is : " + today.plusDays(90).format("YYYY-MM-DD")); } /** What day is 3 months and 5 days from today? */ private void whenIs3Months5DaysFromToday(){ DateTime today = DateTime.today(TimeZone.getDefault()); DateTime result = today.plus(0,3,5,0,0,0,0,DateTime.DayOverflow.FirstDay); log("3 months and 5 days from today is : " + result.format("YYYY-MM-DD")); } /** Current number of hours difference between Paris, France and Perth, Australia. */ private void hoursDifferenceBetweenParisAndPerth(){ //this assumes the time diff is a whole number of hours; other styles are possible DateTime paris = DateTime.now(TimeZone.getTimeZone("Europe/Paris")); DateTime perth = DateTime.now(TimeZone.getTimeZone("Australia/Perth")); int result = perth.getHour() - paris.getHour(); if( result < 0 ) { result = result + 24; } log("Numbers of hours difference between Paris and Perth : " + result); } /** How many weeks is it since Sep 6, 2010? */ private void weeksSinceStart(){ DateTime today = DateTime.today(TimeZone.getDefault()); DateTime startOfProject = DateTime.forDateOnly(2010, 9, 6); int result = today.getWeekIndex() - startOfProject.getWeekIndex(); log("The number of weeks since Sep 6, 2010 : " + result); } /** How much time till midnight? */ private void timeTillMidnight(){ DateTime now = DateTime.now(TimeZone.getDefault()); DateTime midnight = now.plusDays(1).getStartOfDay(); long result = now.numSecondsFrom(midnight); log("This many seconds till midnight : " + result); } /** Format using ISO style. */ private void imitateISOFormat(){ DateTime now = DateTime.now(TimeZone.getDefault()); log("Output using an ISO format: " + now.format("YYYY-MM-DDThh:mm:ss")); } private void firstDayOfThisWeek(){ DateTime today = DateTime.today(TimeZone.getDefault()); DateTime firstDayThisWeek = today; //start value int todaysWeekday = today.getWeekDay(); int SUNDAY = 1; if(todaysWeekday > SUNDAY){ int numDaysFromSunday = todaysWeekday - SUNDAY; firstDayThisWeek = today.minusDays(numDaysFromSunday); } log("The first day of this week is : " + firstDayThisWeek); } /** For how many years has the JDK date-time API been suctorial? */ private void jdkDatesSuctorial(){ DateTime today = DateTime.today(TimeZone.getDefault()); DateTime jdkFirstPublished = DateTime.forDateOnly(1996, 1, 23); int result = today.getYear() - jdkFirstPublished.getYear(); log("The number of years the JDK date-time API has been suctorial : " + result); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy