HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Dates And Calendars"
Vol. 6, Issue 6, p. 136

	



Listing 1

import java.text.DateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
// display inauguration day as a String
public class Inauguration1
{
static public void main(String [] args)
{
// inauguration day is Thursday 2005 January 20 noon Eastern
                Standard Time.
TimeZone est = TimeZone.getTimeZone ("America/New_York");
GregorianCalendar inauguration = new GregorianCalendar(est);
inauguration.set(2005, Calendar.JANUARY, 20, 12, 0, 0);


// locale specific: e.g. Jan 20, 2005
DateFormat df = DateFormat.getDateInstance();


// set timezone
df.setCalendar(inauguration);


// set timestamp
String dateString = df.format(inauguration.getTime());
System.out.println (dateString);
        }
}


Listing 2

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


// display inauguration day as a String
public class Inauguration2
{
static public void main(String [] args)
{
// inauguration day is Thursday 2005 January 20 noon Eastern
         Standard Time.
TimeZone est = TimeZone.getTimeZone ("America/New_York");
GregorianCalendar inauguration = new GregorianCalendar(est);
inauguration.set(2005, Calendar.JANUARY, 20, 12, 0, 0);
// mask for: Thursday 2005/01/20 12:00:00 PM EST Eastern Standard Time
SimpleDateFormat sdf = new SimpleDateFormat("EEEE yyyy/MM/dd hh:mm:ss aa zz : zzzzzz");


// set timezone
sdf.setCalendar(inauguration);


// set timestamp
String dateString = sdf.format(inauguration .getTime());
System.out.println (dateString);
        }
}



Listing 3

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
// parse a date
public class Inauguration3
{
static public void main(String [] args)
{
String dateString = "2005/01/20";
// define the format of the date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
// set timezone to use in interpreting the date
TimeZone est = TimeZone.getTimeZone ("America/New_York");
GregorianCalendar inauguration = new GregorianCalendar(est);
sdf.setCalendar(inauguration);
try
{
// set timestamp
Date date = sdf.parse (dateString);
inauguration.setTime(date);
// inauguration is now ready with both timezone and timestamp
}
catch (ParseException e)
{
System.out.println ("bad date");
}
}
}


Listing 4

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
// Hours until the next presidential inauguration
public class Inauguration4
{
static final int MILLIS_PER_HOUR = 60 * 60 * 1000;
static public void main(String [] args)
{
// inauguration day is Thursday 2005 January 20 noon Eastern Standard Time.
TimeZone est = TimeZone.getTimeZone ("America/New_York");
GregorianCalendar inauguration = new GregorianCalendar(est);
inauguration.set(2005, Calendar.JANUARY, 20, 12, 0, 0);
// now is current time, using default timezone
GregorianCalendar now = new GregorianCalendar();
// milliseconds since 1970 Jan 1
long epochInauguration = inauguration.getTime( ).getTime( );
long epochNow = now.getTime().getTime();


double hours = (double) (epochInauguration - epochNow)/
                MILLIS_PER_HOUR;
System.out.println (hours + " hours until the inauguration.");
}
}



Listing 5 

import cmp.business.BigDate;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
/**
* How Long until Christmas?
*/
public class Christmas
{
static final int MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;
public static void main (String [] args)
{
// How long till Christmas? (Sun's way.)
// this code will give a negative number just after Christmas.
System.out.println ("Your child asks, how long is it until "
+ "Christmas?");
System.out.println ("Here are the possible answers you might give"
+ " to an American child:");
GregorianCalendar now = new GregorianCalendar();
int thisYear = now.get(Calendar .YEAR);


// You may open presents at 7 AM December 25, local time
System.out.println ("You may open your presents at 7 AM Christmas"
+ " morning." );
GregorianCalendar christmas = new GregorianCalendar(thisYear, Calendar.DECEMBER, 25, 7, 0, 0);
// millis since 1970 Jan 1
long christmasTimeStamp = christmas.getTime( ).getTime( );
long nowTimeStamp = now.getTime() .getTime();
double dayUnitsDiff = (christmasTimeStamp - nowTimeStamp) /
(double) MILLISECONDS_PER_DAY;
System.out.println ("1. It is " + dayUnitsDiff + " day units of 24"
+ " hours until you may open your presents.");
System.out.println ("2. It is " + Math.ceil(dayUnitsDiff) + " day"
+ " units of 24 hours rounded up until you may open your "
+ "presents.");
System.out.println ("3. It is " + Math.round(dayUnitsDiff) +
" day units of 24 hours rounded until you may open your "
+ "presents.");
System.out.println ("4. It is " + Math.floor(dayUnitsDiff) +
" day units of 24 hours rounded down until you may open your"
+ " presents.");


int gmtChristmasOrdinal = (int) (christmasTimeStamp /
MILLISECONDS_PER_DAY);
int gmtNowOrdinal = (int) (nowTimeStamp / MILLISECONDS_PER_DAY);
int gmtDiffInDays = gmtChristmasOrdinal - gmtNowOrdinal;
System.out.println ("5. Children in Greenwich have " +
 gmtDiffInDays + " sleeps (midnight crossings) to go until\n" + "you may open your presents.\n" +
"They may open theirs even sooner.");


// For children living in the USA, the timezone offset will be negative.
int christmasZoneOffset = christmas.get(Calendar .ZONE_OFFSET)
                now.get(Calendar .DST_OFFSET);
int localChristmasOrdinal = (int) ((christmasTimeStamp +
                christmasZoneOffset) / MILLISECONDS_PER_DAY);
int nowZoneOffset = now.get(Calendar .ZONE_OFFSET) +
                now.get(Calendar .DST_OFFSET);
int localNowOrdinal = (int) ( (nowTimeStamp + nowZoneOffset) /
                MILLISECONDS_PER_DAY);
int localDiffInDays = localChristmasOrdinal - localNowOrdinal;
System.out.println ("6. You have " + localDiffInDays +
" sleeps (midnight crossings) to go.");


// how long till Christmas? ( with BigDate. )
// this code will give a negative number just after Christmas.
BigDate today = BigDate .localToday();
BigDate nextChristmas = new BigDate(today.getYYYY( ), 12, 25);
System.out.println ("7. Uncle Roedy's Bigdate says it is " + (nextChristmas .getOrdinal() - today.getOrdinal()) +
" sleeps until Christmas.");
int [ ] age = BigDate .age( today, nextChristmas);
System.out.println ("8. Or put another way " + age[0 ] +
" years and " + age [1] + " months and " + age[2] + " days to"
+ "go.");
}
}



  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.