⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testdate.java

📁 java base64
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*
TestDate: Validate BigDate to ensure it is bug-free.
Also demonstrate and  exercise the various methods.

copyright (c) 1997-2008 Roedy Green, Canadian Mind Products
may be copied and used freely for any purpose but military.
Roedy Green
Canadian Mind Products
#101 - 2536 Wark Street
Victoria, BC Canada
V8T 4G8
tel: (250) 361-9093
roedy g at mindprod dotcom
http://mindprod.com

*/
package com.mindprod.common11;

// TestDate.java

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;
import java.util.TimeZone;

/**
 * Validate BigDate to ensure it is bug-free. Also demonstrate and exercise the various methods.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 5.0 2007-10-15 add test for nextXXXDay.
 */
public final class TestDate
    {
    // ------------------------------ FIELDS ------------------------------

    private static int count;

    /**
     * 86,400,000 the number of milliseconds in 24 hour day. Easily fits into an int.
     */
    private static final int MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;

    /**
     * days of the week 3 letter abbreviations in English. Index Sunday = 0.
     */
    private static final String[] shortDayName =
            { "sun", "mon", "tue", "wed", "thu", "fri", "sat" };

    // -------------------------- STATIC METHODS --------------------------

    /**
     * show off some of the things BigDate can do See also com.mindprod.holidays.Holiday to compute various holidays and
     * com.mindprod.holidays.IsHoliday to tell if a given day is a holiday.
     *
     * @noinspection PointlessArithmeticExpression
     */
    private static void demoDate()
        {
        sep();
        {
        // Daylight saving (2:00 A.M. on the second Sunday in March, and
        // 2:00 A.M. on the first Sunday in November. Canada follows the USA.
        for ( int year = 2008; year <= 2020; year++ )
            {
            /* daylight saving starts  2:00 A.M. on the second Sunday in March */
            System.out
                    .println( "In " + year + " daylight saving starts 2AM on March " + BigDate.nthXXXDay( 2/* second */,
                            BigDate.SUNDAY,
                            year,
                            BigDate.MARCH ) );

            /* daylight saving ends  2:00 A.M. on the first Sunday in November. */
            System.out
                    .println( "In " + year + " daylight saving ends 2AM on November " + BigDate.nthXXXDay( 1/* first */,
                            BigDate.SUNDAY,
                            year,
                            BigDate.NOVEMBER ) );
            }
        }
        sep();
        {
        // Using traditional tools
        Date date = new Date();
        System.out.println( "Now, according to Java Date is : " + date );
        System.out
                .println( "That is "
                          + date.getTime()
                          + " milliseconds since 1970-01-01." );
        System.out
                .println(
                        "In contrast, BigDate works with pure dates, without times." );
        System.out
                .println( "Today, according to BigDate is : "
                          + BigDate.localToday()
                          + "." );
        }
        sep();
        {
        // Test Normalise
        System.out
                .println( "How an invalid date can be normalised:" );
        testNormalize( 2007, 2, 34 );
        }
        sep();
        {
        // what is today's date in American format
        System.out
                .println( "Today American style is " + BigDate.localToday()
                        .toDowMMDDYY() );
        }
        sep();
        {
        // what is today's date in iso format
        System.out
                .println( "Today ISO style is " + BigDate.localToday()
                        .toString() );
        }
        sep();
        {
        // What date is it in Greenwich?
        BigDate bigDate = BigDate.UTCToday();
        System.out
                .println( "Today in Greenwich England = "
                          + bigDate.toString()
                          + "." );
        /* 0=Sunday to 6=Saturday */
        int dayOfWeek = bigDate.getDayOfWeek();
        String[] daysOfTheWeek = {
                "Sunday",
                "Monday",
                "Tuesday",
                "Wednesday",
                "Thursday",
                "Friday",
                "Saturday" };
        String dayName = daysOfTheWeek[ dayOfWeek ];
        System.out.println( "In Greenwich it is " + dayName + "." );
        }
        sep();
        {
        // What day of the week is today?
        // display ISO 8601:1988 international standard format: yyyy-mm-dd.
        BigDate bigDate = BigDate.localToday();
        System.out.println( "Today = " + bigDate.toString() + "." );
        /* 0=Sunday to 6=Saturday */
        int dayOfWeek = bigDate.getDayOfWeek();
        String[] daysOfTheWeek = {
                "Sunday",
                "Monday",
                "Tuesday",
                "Wednesday",
                "Thursday",
                "Friday",
                "Saturday" };
        String dayName = daysOfTheWeek[ dayOfWeek ];
        System.out.println( "Today is " + dayName + "." );
        System.out
                .println( "Today is dayOfWeek number "
                          + bigDate.getDayOfWeek()
                          + " if Sunday=0." );
        System.out
                .println( "Today is dayOfWeek number "
                          + bigDate.getCalendarDayOfWeek()
                          + " if Sunday=1." );
        }
        pause();
        {
        // what season is it?
        String[] seasons = new String[] { "spring", "summer", "fall", "winter" };
        BigDate today = BigDate.localToday();
        System.out
                .println( "It is " + seasons[ today.getSeason() ] );
        System.out.println( "Season for months are: " );
        for ( int i = 1; i <= 12; i++ )
            {
            BigDate seasonStart = new BigDate( today.getYYYY(), i, 1 );
            System.out.println( i + " " + seasons[ seasonStart.getSeason() ] );
            }
        }
        pause();
        {
        // What Date is Cobol yyddd bigDate 99360?
        BigDate bigDate = new BigDate( 99 + 1900, 1, 360, BigDate.NORMALISE );
        System.out
                .println( "COBOL-style yyddd date 99360 = "
                          + bigDate.toString()
                          + "." );
        }
        sep();
        {
        // How do you convert Java timestamps into Windows timestamps.
        // Java timestamps use 64-bit milliseconds since 1970 GMT.
        // Windows timestamps use 64-bit value representing the number
        // of 100-nanosecond intervals since January 1, 1601.
        int windowsBase = BigDate.toOrdinal( 1601, 1, 1 );
        int javaBase = BigDate.toOrdinal( 1970, 1, 1 );
        int daysDifference = javaBase - windowsBase;
        // but Microsoft forgot that
        // 86,400,000 = 1000 * 60 * 60 * 24 = milliseconds per day
        long millisDifference = daysDifference * 86400000L;
        System.out
                .println( "To convert Java Timestamps to Windows Timestamps:" );
        System.out
                .println( "windows = ( java + "
                          + millisDifference
                          + "L ) * 10000" );
        }
        sep();
        {
        // Display a BigDate with SimpleDateFormat using local time
        BigDate bigDate = new BigDate( 1999, 12, 31 );
        Date date = bigDate.getLocalDate();
        SimpleDateFormat sdf =
                new SimpleDateFormat(
                        "EEEE yyyy/MM/dd G hh:mm:ss aa zz : zzzzzz" );
        sdf.setTimeZone( TimeZone.getDefault() );// local time
        String dateString = sdf.format( date );
        System.out.println( "Local SimpleDateFormat: " + dateString );
        }
        sep();
        {
        // Display a BigDate with SimpleDateFormat using UTC (Greenwich GMT)
        // time
        BigDate bigDate = new BigDate( 1999, 12, 31 );
        Date date = bigDate.getUTCDate();
        SimpleDateFormat sdf =
                new SimpleDateFormat(
                        "EEEE yyyy/MM/dd G hh:mm:ss aa zz : zzzzzz" );
        sdf.setTimeZone( TimeZone.getTimeZone( "GMT" ) );// GMT time
        String dateString = sdf.format( date );
        System.out.println( "UTC/GMT SimpleDateFormat: " + dateString );
        }
        sep();
        {
        // Display a BigDate with default-locale DateFormat using UTC
        // (Greenwich GMT) time
        BigDate bigDate = new BigDate( 1999, 12, 31 );
        Date date = bigDate.getUTCDate();
        DateFormat df = DateFormat.getDateInstance();
        df.setTimeZone( TimeZone.getTimeZone( "GMT" ) );// GMT time
        String dateString = df.format( date );
        System.out.println( "UTC/GMT locale DateFormat: " + dateString );
        }
        sep();
        {
        // What are the earliest and latest dates BigDate can handle
        BigDate bigDate = new BigDate( BigDate.MIN_ORDINAL );
        System.out
                .println( "Earliest BigDate possible is "
                          + bigDate.toString() );
        bigDate.setOrdinal( BigDate.MAX_ORDINAL );
        System.out
                .println( "Latest BigDate possible is " + bigDate.toString() );
        }
        sep();
        {
        // What is the last day of February in 2000, preferred method
        BigDate bigDate = new BigDate( 2000, 3, 1 );/* 2000, March 1 */
        bigDate.addDays( -1 );/* last day of Feb */
        System.out
                .println( "Last day in Feb 2000 = "
                          + bigDate.toString()
                          + "." );
        }
        sep();
        {
        // What is the last day of February in 2004, alternate less safe,
        // no-check method
        BigDate bigDate = new BigDate( 2004, 2 + 1, 1 - 1, BigDate.NORMALIZE );/*
                                                                                 * 2004,
                                                                                 * March
                                                                                 * 0
                                                                                 */
        System.out
                .println( "Last day in Feb 2004 = "
                          + bigDate.toString()
                          + "." );
        }
        sep();
        {
        // What is the last day of February in 2006, generic method.
        BigDate bigDate = new BigDate( 2006, 2, 1 );/* 2004, Feb 1 */
        bigDate.addDays( BigDate.daysInMonth( 2, 2006 ) - 1 );
        System.out
                .println( "Last day in Feb 2006 = "
                          + bigDate.toString()
                          + "." );
        }
        sep();
        {
        // What Date was it yesterday?
        BigDate bigDate = BigDate.localToday();
        bigDate.addDays( -1 );
        System.out.println( "Yesterday = " + bigDate.toString() + "." );
        }
        sep();
        {
        // What Date will it be tomorrow?
        BigDate bigDate = BigDate.localToday();
        bigDate.addDays( 1 );
        System.out.println( "Tomorrow = " + bigDate.toString() + "." );
        }
        sep();
        {
        // What Date will it be 1000 days from now?
        BigDate bigDate = BigDate.localToday();
        bigDate.addDays( 1000 );
        System.out.println( "Today+1000 days = " + bigDate.toString() + "." );
        }
        sep();
        {
        // What date will it be 50 months after 1998-01-03
        // You can think of this as how to turn a possibly invalid date into
        // the equivalent valid one.
        BigDate bigDate = new BigDate( 1998, 1 + 50, 3, BigDate.NORMALISE );
        System.out
                .println( "1998-01-03+50 months = "
                          + bigDate.toString()
                          + "." );
        }
        sep();
        {
        // What date will it be 60 months from today
        BigDate today = BigDate.localToday();
        BigDate bigDate =
                new BigDate( today.getYYYY(), today.getMM() + 60, today
                        .getDD(), BigDate.NORMALISE );
        System.out
                .println( "60 months from today will be "
                          + bigDate.toString()
                          + "." );
        }

        sep();
        {
        // Was 1830-02-29 a valid date?

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -