From: Bing Long Date: Tue, 14 May 2013 20:39:25 +0000 (+0000) Subject: ICU-10113 Added DateIntervalFormatSample.java. X-Git-Tag: milestone-59-0-1~2899 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=344f86cc163e770396d5aafea44a9f6ca1c437ec;p=icu ICU-10113 Added DateIntervalFormatSample.java. Updated the API document for DateIntervalFormat->getInstance(String skeleton, Locale locale) and DateIntervalFormat->getInstance(String skeleton, Locale locale, DateIntervalInfo dtitvinf) X-SVN-Rev: 33652 --- diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/DateIntervalFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/DateIntervalFormat.java index dc67ad7b034..1e089ae2b00 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/DateIntervalFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/DateIntervalFormat.java @@ -398,6 +398,7 @@ public class DateIntervalFormat extends UFormat { * This is a convenient override of * getInstance(String skeleton, ULocale locale) * + *

Example code:{@.jcite com.ibm.icu.samples.text.dateintervalformat.DateIntervalFormatSample:---dtitvfmtPreDefinedExample} * @param skeleton the skeleton on which interval format based. * @param locale the given locale * @return a date time interval formatter. @@ -476,7 +477,8 @@ public class DateIntervalFormat extends UFormat { * * This is a convenient override of * getInstance(String skeleton, ULocale locale, DateIntervalInfo dtitvinf) - * + * + *

Example code:{@.jcite com.ibm.icu.samples.text.dateintervalformat.DateIntervalFormatSample:---dtitvfmtCustomizedExample} * @param skeleton the skeleton on which interval format based. * @param locale the given locale * @param dtitvinf the DateIntervalInfo object to be adopted. diff --git a/icu4j/samples/src/com/ibm/icu/samples/text/dateintervalformat/DateIntervalFormatSample.java b/icu4j/samples/src/com/ibm/icu/samples/text/dateintervalformat/DateIntervalFormatSample.java new file mode 100644 index 00000000000..cf6c6af11c7 --- /dev/null +++ b/icu4j/samples/src/com/ibm/icu/samples/text/dateintervalformat/DateIntervalFormatSample.java @@ -0,0 +1,124 @@ +/* + ******************************************************************************* + * Copyright (C) 2013, International Business Machines Corporation and * + * others. All Rights Reserved. * + ******************************************************************************* + */ +package com.ibm.icu.samples.text.dateintervalformat; + + + +import java.util.Date; + +import com.ibm.icu.text.DateFormat; +import com.ibm.icu.text.DateIntervalFormat; +import com.ibm.icu.text.DateIntervalInfo; +import com.ibm.icu.util.Calendar; +import com.ibm.icu.util.DateInterval; +import com.ibm.icu.util.GregorianCalendar; +import com.ibm.icu.util.ULocale; + +public class DateIntervalFormatSample{ + + public static void main (String[] args){ + + dtitvfmtPreDefined(); + dtitvfmtCustomized(); + + } + + public static void dtitvfmtPreDefined() + { + System.out.println("=============================================================================="); + System.out.println(" dtitvfmtPreDefined()"); + System.out.println(); + System.out.println(" Use DateIntervalFormat to get Date interval format for pre-defined skeletons:"); + System.out.println(" yMMMd, MMMd per locale"); + System.out.println("=============================================================================="); + // ---dtitvfmtPreDefinedExample + final Date date[] = { + new GregorianCalendar(2007,10,10,10,10,10).getTime(), + new GregorianCalendar(2008,10,10,10,10,10).getTime(), + new GregorianCalendar(2008,11,10,10,10,10).getTime(), + new GregorianCalendar(2008,11,10,15,10,10).getTime(), + }; + final DateInterval dtitv[] = { + new DateInterval(date[0].getTime(),date[1].getTime()), + new DateInterval(date[1].getTime(),date[2].getTime()), + new DateInterval(date[2].getTime(),date[3].getTime()), + }; + final String [] skeletons = { + DateFormat.YEAR_ABBR_MONTH_DAY, + DateFormat.MONTH_DAY, + DateFormat.HOUR_MINUTE, + }; + System.out.printf("%-15s%-35s%-35s%-35s%-35s\n", "Skeleton", "from","to","Date Interval in en_US", "Date Interval in Ja"); + int i=0; + for (String skeleton:skeletons) { + System.out.printf("%-15s%-35s%-35s", skeleton,date[i].toString(), date[i+1].toString()); + DateIntervalFormat dtitvfmtEn = DateIntervalFormat.getInstance(skeleton, ULocale.ENGLISH); + DateIntervalFormat dtitvfmtJa = DateIntervalFormat.getInstance(skeleton, ULocale.JAPANESE); + System.out.printf("%-35s%-35s\n", dtitvfmtEn.format(dtitv[i]),dtitvfmtJa.format(dtitv[i])); + i++; + } + /** output of the sample code: + ********************************************************************************************************************************************************* + Skeleton from to Date Interval in en_US Date Interval in Ja + yMMMd Sat Nov 10 10:10:10 EST 2007 Mon Nov 10 10:10:10 EST 2008 Nov 10, 2007 – Nov 10, 2008 2007年11月10日~2008年11月10日 + MMMMd Mon Nov 10 10:10:10 EST 2008 Wed Dec 10 10:10:10 EST 2008 November 10 – December 10 11月10日~12月10日 + jm Wed Dec 10 10:10:10 EST 2008 Wed Dec 10 15:10:10 EST 2008 10:10 AM – 3:10 PM 10:10~15:10 + + *********************************************************************************************************************************************************/ + // ---dtitvfmtPreDefinedExample +} + public static void dtitvfmtCustomized() + { + System.out.println("================================================================================"); + System.out.println(" dtitvfmtCustomized()"); + System.out.println(); + System.out.println(" Use DateIntervalFormat to create customized date interval format for yMMMd, Hm"); + System.out.println("================================================================================"); + // ---dtitvfmtCustomizedExample + final Date date[] = { + new GregorianCalendar(2007,9,10,10,10,10).getTime(), + new GregorianCalendar(2007,10,10,10,10,10).getTime(), + new GregorianCalendar(2007,10,10,22,10,10).getTime(), + }; + final DateInterval dtitv[] = { + new DateInterval(date[0].getTime(),date[1].getTime()), + new DateInterval(date[1].getTime(),date[2].getTime()), + }; + final String [] skeletons = { + DateFormat.YEAR_ABBR_MONTH_DAY, + DateFormat.HOUR24_MINUTE, + }; + System.out.printf("%-15s%-35s%-35s%-45s%-35s\n", "Skeleton", "from","to", "Date Interval in en_US", "Date Interval in Ja"); + // Create an empty DateIntervalInfo object + DateIntervalInfo dtitvinf = new DateIntervalInfo(ULocale.ENGLISH); + // Set Date Time internal pattern for MONTH, DAY_OF_MONTH, HOUR_OF_DAY + dtitvinf.setIntervalPattern("yMMMd", Calendar.MONTH, "y 'Diff' MMM d --- MMM d"); + dtitvinf.setIntervalPattern("Hm", Calendar.HOUR_OF_DAY, "yyyy MMM d HH:mm ~ HH:mm"); + // Set fallback interval pattern + dtitvinf.setFallbackIntervalPattern("{0} ~~~ {1}"); + // Get the DateIntervalFormat with the custom pattern + for (String skeleton:skeletons){ + for (int i=0;i<2;i++) { + System.out.printf("%-15s%-35s%-35s", skeleton,date[i].toString(), date[i+1].toString()); + DateIntervalFormat dtitvfmtEn = DateIntervalFormat.getInstance(skeleton,ULocale.ENGLISH,dtitvinf); + DateIntervalFormat dtitvfmtJa = DateIntervalFormat.getInstance(skeleton,ULocale.JAPANESE,dtitvinf); + System.out.printf("%-45s%-35s\n", dtitvfmtEn.format(dtitv[i]),dtitvfmtJa.format(dtitv[i])); + } + } + /** output of the sample code: + ************************************************************************************************************************************************************************* + Skeleton from to Date Interval in en_US Date Interval in Ja + yMMMd Wed Oct 10 10:10:10 EDT 2007 Sat Nov 10 10:10:10 EST 2007 2007 Diff Oct 10 --- Nov 10 2007 Diff 10月 10 --- 11月 10 + yMMMd Sat Nov 10 10:10:10 EST 2007 Sat Nov 10 22:10:10 EST 2007 Nov 10, 2007 2007年11月10日 + Hm Wed Oct 10 10:10:10 EDT 2007 Sat Nov 10 10:10:10 EST 2007 10/10/2007, 10:10 ~~~ 11/10/2007, 10:10 2007/10/10 10:10 ~~~ 2007/11/10 10:10 + Hm Sat Nov 10 10:10:10 EST 2007 Sat Nov 10 22:10:10 EST 2007 2007 Nov 10 10:10 ~ 22:10 2007 11月 10 10:10 ~ 22:10 + *************************************************************************************************************************************************************************/ + // ---dtitvfmtCustomizedExample + + } +} +