Logo
calendar java
mainfo for calendar java: videos, blogs, definitions, info, downloads, links, Q&A
calendar java


 
 
Advertisment

 
Wikipedia

The Sun Java System Calendar Server (WCAP) is Sun's calendar (scheduling) server. History and use Sun's Java System Calendar Server has a long history, drawing technology from Sun Internet Calendar Server (SICS) and Netscape Calendar Server (NCS...
http://en.wikipedia.org/wiki/Sun_Java_Calendar_Server
 
Links

 
 
Blogs

 
Videos

ne-zhrat-obedy.avi


animated_calendar.swf


5001P-Red.swf


 
 
Downloads

 
Definition

 
Questions & Answers

How do I use Calendar in Java?
I'm referring to the java.util.Calendar package. I've been using Date and I'm pretty happy with it but it's been deprecated so I'd like to learn how to use Calendar. I've read about it on Sun's website but I'm still not quite comfortable with it. All I need to do is get the current month/day/year and print it out, so if you know at least that your help is still appreciated.

// Get calendar instance Calendar cal = Calendar.getInstance(); int day = cal.get( Calendar.DATE); int month = cal.get( Calendar.MONTH); int year = cal.get( Calendar.YEAR); Watch out for the month value though because January is 0 not 1 Set a value by cal.set( Calendar.MINUTE, 30 );



How to Display a Java Calendar?
I'm trying to display a java calendar that takes the input of the user with JOptionPane of what year they would like to display and the start day of January 1st. I am really lost on how to do this any help would be appreciated. I am trying to display the whole year with the month on top then the days below that then the dates under that. A standard calendar basically.

Here is a programme that reads the user's month/year from the command line and creates a calendar for each month from the user's month until the end of the year. The calendars are then printed, three across, to the standard output device. You can tweek the main to get the data however you need it to come in. The programme is not pretty (only took a couple of hours) but it will give you an idea of what you need to do. import java.util.*; import java.lang.*; import java.io.*; // display a calendar class ptest { public static String mnames[] = { "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; public static int days_in_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // returns the day number of the week for a given month/day/year // sunday is day 7; monday is 1 public static int mdy2dow( int m, int d, int y ) { int x; if( y > 1900 ) y -= 1900; // need only years since 1900 (2000 is 100) x = (((m - 1) * 31 ) + d) + (y * 365); if( m > 2 ) x += (y/4) - (((m * 4) + 23) / 10); else x += (y - 1)/4; x %= 7; return x == 0 ? 7 : x; // monday is 1 } // convert string to int/long and prevent bloody number exception error at run time // converts like C atoi -- an immediate non-numeric converts to 0 not a bloody // exception public static int atoi( String s ) { try { return Integer.parseInt( s ); } catch( NumberFormatException e ) { return 0; } } // --------------- class vars and methods ------------------------------ private int week[][]; private int nweeks; private int month; private int year; ptest( int m, int y ) { int dow = -1; // day of week that month starts on int i; int w = 0; int d = 1; int dim; // days in the month month = m; year = y; week = new int[5][7]; nweeks = 0; dow = mdy2dow( month, 1, year ) - 1; // day of week month starts on; make zero based dim = days_in_month[month-1]; // days in the month if( month == 2 && year%4 == 0 ) // this works unti 2100 dim++; for( i = 0; i < dow; i++ ) week[w][i] = 0; // filler -- will print blanks for( i = dow; i < 7; i++ ) // first, partial?, week week[w][i] = d++; for( w = 1; w < 5; w++ ) // fill in remaining weeks { for( i = 0; i < 7; i++ ) { week[w][i] = d <= dim ? d : 0; d++; } } } // print a week, allowing multiple months to be printed // across the screen rather than in a column down the screen private void print_week( int w ) { int i; for( i = 0; i < 7; i++ ) { if( week[w][i] > 0 ) System.out.printf( "%2d ", week[w][i] ); else System.out.printf( " " ); //0s translate to blanks } System.out.printf( " " ); } private void print_header1( ) { System.out.printf( " %9s %4d ", mnames[month], year ); } private void print_header2( ) { System.out.printf( "Mo Tu We Th Fr Sa Su " ); } public static void main( String argv[] ) { int month = 1; int year = 2008; int i; ptest cal_list[]; ptest cal; cal_list = new ptest[12]; if( argv.length == 1 ) // assume year year = atoi( argv[0] ); else { month = atoi( argv[0] ); year = atoi( argv[1] ); } //System.out.printf( "month=%d year=%d ", month, year ); while( month <= 12 ) { for( i = month; i <= 12 && i < month+3; i++ ) cal_list[i-1] = new ptest( i, year ); // not graceful, but simple for( i = month; i <= 12 && i < month+3; i++ ) cal_list[i-1].print_header1(); System.out.printf( " " ); for( i = month; i <= 12 && i < month+3; i++ ) cal_list[i-1].print_header2(); System.out.printf( " " ); for( i = month; i <= 12 && i < month+3; i++ ) cal_list[i-1].print_week( 0 ); System.out.printf( " " ); for( i = month; i <= 12 && i < month+3; i++ ) cal_list[i-1].print_week( 1 ); System.out.printf( " " ); for( i = month; i <= 12 && i < month+3; i++ ) cal_list[i-1].print_week( 2 ); System.out.printf( " " ); for( i = month; i <= 12 && i < month+3; i++ ) cal_list[i-1].print_week( 3 ); System.out.printf( " " ); for( i = month; i <= 12 && i < month+3; i++ ) cal_list[i-1].print_week( 4 ); System.out.printf( " " ); month += 3; } } }



How do I calculate the difference between two calendar dates in Java?
It must be done manually without using date or calendar methods. Or where can I obtain the source code that forms those methods? I'm trying to write a program that does this manually without date or calendar methods. Basically, I get confused when the dates fall in two different months, or even years.

You don't need the source code (since you can't modify the variables in the object so it won't do you much good). All you need is the interface which is available on the sun website. I've linked to it in the sources. Good luck.