package com.javaranch.common ; import java.util.* ; /** Gregorian Date, international format (YYYY/MM/DD).
These dates are stored as Year, Month and Day. Not very memory efficient, but good for converting to strings, or quickly extracting the day of the month.
See the complimentary class JDate for memory efficiency or doing date math.
This object uses the international YYYY/MM/DD format. See ADate for MM/DD/YYYY format.
- - - - - - - - - - - - - - - - -
Copyright (c) 1998-2000 Paul Wheaton
You are welcome to do whatever you want to with this source file provided that you maintain this comment fragment (between the dashed lines). Modify it, change the package name, change the class name ... personal or business use ... sell it, share it ... add a copyright for the portions you add ...
My goal in giving this away and maintaining the copyright is to hopefully direct developers back to JavaRanch.
The original source can be found at JavaRanch
- - - - - - - - - - - - - - - - -
@author Paul Wheaton */ public class GDate { private int y ; private int m ; private int d ; /** Create a Gregorian Date object with a default of today's date.
*/ public GDate() { setToToday(); } /** Create a Gregorian Date object with a specific date.
*/ public GDate( int year , int month , int day ) { y = year ; m = month ; d = day ; } /** Create a Gregorian Date object with a specific Julian date.
*/ public GDate( JDate j ) { this( j.getGDate() ); } /** Create a Gregorian Date object from another Gregorian Date object (GDate or ADate).
*/ public GDate( GDate d ) { this.y = d.y ; this.m = d.m ; this.d = d.d ; } /** Create a Gregorian Date object with a loosely formatted text date.
The general format is YYYY/MM/DD.
Any type of delimiter can be used (slash, hyphen, space, etc.). The month and date does not have to be two digits in length. The year does not have to be four digits in length, although "98" will be interpretted as 0098, not 1998 or 2098.
Invalid dates are accepted.
If you leave out the day of the month, the first is assumed.
If you leave out the month, January is assumed.
If the string is blank, the current date is used.
*/ public GDate( String textDate ) { set( textDate ); } /** Force the date based on the contents of a string.
The general format is YYYY/MM/DD.
Any type of delimiter can be used (slash, hyphen, space, etc.). The month and date does not have to be two digits in length. The year does not have to be four digits in length, although "98" will be interpretted as 0098, not 1998 or 2098.
Invalid dates are accepted.
If you leave out the day of the month, the first is assumed.
If you leave out the month, January is assumed.
If the string is blank, the current date is used.
*/ public void set( String textDate ) { Str s = new Str( textDate ); for( int i = 0 ; i < s.length() ; i++ ) { if ( ! Numbers.inRange( s.get( i ) , '0' , '9' ) ) { s.set( i , ' ' ); } } s.trim(); s.removeDoubleSpaces(); if ( s.length() == 0 ) { setToToday(); } else { y = Str.atoi( s.extractWord().toString() ); m = 1 ; d = 1 ; if ( s.length() > 0 ) { m = Str.atoi( s.extractWord().toString() ); } if ( s.length() > 0 ) { d = Str.atoi( s.extractWord().toString() ); } } } /** Set this object to reflect today's date.
*/ public void setToToday() { GregorianCalendar gc = new GregorianCalendar() ; y = gc.get( GregorianCalendar.YEAR ); m = gc.get( GregorianCalendar.MONTH ) + 1 ; d = gc.get( GregorianCalendar.DAY_OF_MONTH ); } public void setYear( int year ) { y = year ; } /** Expects values 1..12 although invalid values are accepted.
*/ public void setMonth( int month ) { m = month ; } /** Expects values 1..31 although invalid values are accepted.
*/ public void setDay( int day ) { d = day ; } public int getYear() { return y ; } public int getMonth() { return m ; } private static final String[] monthName = { null , "January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December" }; /** Convert a number 1 through 12 to a String "January" through "December".
Passing in values outside of the 1 through 12 range will return an empty string.
*/ public static String getMonthString( int month ) { String returnVal = "" ; if ( Numbers.inRange( month , 1 , 12 ) ) { returnVal = monthName[ month ]; } return returnVal ; } /** Get the full month name.
If the current month is outside of the 1 through 12 range, an empty string will be returned.
Month names will be capitalized properly. e.g. "January".
*/ public String getMonthString() { return getMonthString( m ); } /** Return the day of the month (1..31).
*/ public int getDay() { return d ; } /** Such as "05-01-2000".
@deprecated */ public String getShortFormat() { StringBuffer buffy = new StringBuffer( String.valueOf( m ) ); if ( buffy.length() == 1 ) { buffy.insert( 0 , '0' ); } buffy.append('-'); buffy.append( String.valueOf( d ) ); if ( buffy.length() == 4 ) { buffy.insert( 3 , '0' ); } buffy.append('-'); buffy.append( String.valueOf( y ) ); return buffy.toString() ; } /** Such as "2000/05/01".
*/ public String getFixedFormat() { StringBuffer buffy = new StringBuffer( String.valueOf( y ) ); while ( buffy.length() < 4 ) { buffy.insert( 0 , '0' ); } buffy.append( '-' ); buffy.append( String.valueOf( m ) ); if ( buffy.length() == 6 ) { buffy.insert( 5 , '0' ); } buffy.append('-'); buffy.append( String.valueOf( d ) ); if ( buffy.length() == 9 ) { buffy.insert( 8 , '0' ); } return buffy.toString() ; } /** Returns a date formatted like "2000/7/24".
*/ public static String toString( GDate g ) { return g.getYear() + "/" + g.getMonth() + "/" + g.getDay(); } /** Returns a date formatted like "2000/7/24".
*/ public String toString() { return toString( this ); } /** Returns a date formatted like "September 22, 1999".
*/ public String getLongFormat() { StringBuffer buffy = new StringBuffer( monthName[ m ] ); buffy.append(' '); buffy.append( String.valueOf( d ) ); buffy.append(", "); buffy.append( String.valueOf( y ) ); return buffy.toString(); } /** Get a copy of this object.
*/ public Object clone() { return new GDate( this ); } /** Provided for testing purposes only.
*/ public static void unitTest( TestLog log ) { // xxx need tests } }