Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Java in General
Calendar 1st day of week changes?
Roger Graff
Ranch Hand
Posts: 112
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Anyone want to guess why the first day of the week is changing on me?
import java.text.*; import java.util.*; public class TestMe{ public static final boolean DEBUG = false; public static void main(String[] args ){ TestMe test = null; try{ test = new TestMe(); // 2005 Date seed3 = test.toDate( "03/27/2005", "MM/dd/yyyy" ); test.calendarTest( seed3 ); Date seed4 = test.toDate( "03/28/2005", "MM/dd/yyyy" ); test.calendarTest( seed4 ); test.display(""); // 2005 Date seed5 = test.toDate( "04/03/2005", "MM/dd/yyyy" ); test.calendarTest( seed5 ); Date seed6 = test.toDate( "04/04/2005", "MM/dd/yyyy" ); test.calendarTest( seed6 ); test.display(""); //2005 Date seed7 = test.toDate( "04/10/2005", "MM/dd/yyyy" ); test.calendarTest( seed7 ); Date seed8 = test.toDate( "04/11/2005", "MM/dd/yyyy" ); test.calendarTest( seed8 ); test.display(""); }catch(Exception e ){ test.display( "Exception in MAIN" ); } // try-catch } // main private String dateToString( Date date, String pattern ) throws Exception{ if (DEBUG) display( "dateToString()" ); String dateStr = null; SimpleDateFormat format = new SimpleDateFormat( pattern ); dateStr = format.format( date ); return dateStr; } // dateToString private Date toDate( String dateStr, String pattern ) throws Exception{ if (DEBUG) display( "toDate()" ); SimpleDateFormat format = new SimpleDateFormat( pattern ); Date date = format.parse( dateStr ); return date; } // toDate public void calendarTest( Date seed ) throws Exception{ if (DEBUG) display( "calendarTest()" ); TimeZone timeZone = TimeZone.getTimeZone( "GMT-5" ); Locale locale = Locale.US; GregorianCalendar calendar = new GregorianCalendar( timeZone, locale ); calendar.setTime( seed ); // what day is it right now? // int dayOfWeek = calendar.get( Calendar.DAY_OF_WEEK ); String numPostfix = getPostfix( dayOfWeek ); StringBuffer buffer = new StringBuffer(); buffer.append( dateToString(seed, "EEEEEEEE, MM/dd/yyyy") ); buffer.append( " is the " ); buffer.append( dayOfWeek ); buffer.append( numPostfix ); buffer.append( " day of the week!" ); display( buffer.toString() ); } // calendarTest private String getPostfix( int num ){ String postfix = null; switch( num ){ case 1: postfix = "st"; break; case 2: postfix = "nd"; break; case 3: postfix = "rd"; break; default: postfix = "th"; } // switch return postfix; } // getPostfix private void display( String text ){ System.out.println( text ); } // display } // TestMe
[ June 28, 2005: Message edited by: Roger Graff ]
Roger Graff
Ranch Hand
Posts: 112
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Here's the output:
Sunday, 03/27/2005 is the 1st day of the week!
Monday, 03/28/2005 is the 2nd day of the week!
Sunday, 04/03/2005 is the 1st day of the week!
Monday, 04/04/2005 is the 1st day of the week!
Sunday, 04/10/2005 is the 7th day of the week!
Monday, 04/11/2005 is the 1st day of the week!
Press any key to continue . . .
Roger Graff
Ranch Hand
Posts: 112
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hmmm. I made the following changes and it seems to be working. Shame on me for not using the interface in the first place!
TimeZone timeZone = TimeZone.getTimeZone( "GMT-5" );
Locale locale = Locale.US;
GregorianCalendar calendar = new GregorianCalendar( timeZone, locale );
to
Calendar calendar = Calendar.getInstance();
Roger Graff
Ranch Hand
Posts: 112
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Okay - here's the culprit:
TimeZone timeZone = TimeZone.getTimeZone( "GMT-5" );
Don't get me started about those stupid
light bulbs
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Any Alternative for this CODE !!!!!!
convert date to dd-mm-yyyy pattern
convert a string to java.sql.Date object
Date Format Issue
Formatting date as MMM for japnese locale
More...