how-to

Working with Date

How to get string weekday names ?

For this example you need to use:

import java.util.*;

This example shows how to get string weekday names for particular date.

/* -- create ENGLISH calendar, set dat and get day of week for that date */
    
Calendar cal = Calendar.getInstance( Locale.ENGLISH );
cal.set( 2012, 12-(1), 12 );   /* month is zero based !!! (JDK 1.7)*/
int iDayOfWeek = cal.get( Calendar.DAY_OF_WEEK );

/* -- convert day of week into string */
    
String sShort = DateFormatSymbols.getInstance( Locale.ENGLISH ).getShortWeekdays()[ iDayOfWeek ];
String sLong = DateFormatSymbols.getInstance( Locale.ENGLISH ).getWeekdays()[ iDayOfWeek ];
   
/* -- output data */
    
System.out.println( iDayOfWeek ); 
System.out.println( sShort );    
System.out.println( sLong );    
The output will be:
4
Wed
Wednesday

For Germany`s names get German DateFormatSymbols instance:

String sShort = DateFormatSymbols.getInstance( Locale.GERMAN ).getShortWeekdays()[ iDayOfWeek ];
String sLong = DateFormatSymbols.getInstance( Locale.GERMAN ).getWeekdays()[ iDayOfWeek ];
, and then output will be:
4
Mi
Mittwoch