Skip to Content
avatar image
Former Member

java code to get the date of second sunday of march of every year

Hi all,

i need some java code to get date of second sunday in march and first sunday in november of every year.

this is very urgent

plz help

thanks in advance

jhansi

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

4 Answers

  • Best Answer
    avatar image
    Former Member
    Nov 14, 2007 at 09:51 AM

    Maybe this one is a little bit easier

    import java.util.Calendar;
    import java.util.Date;
    
    public class Test {
    
    	public static void main (String args[]) {
    	
    		System.out.println (getSecondSundayOfMarch());
    		System.out.println (getFirstSundayOfNovember());
    		
    	}
    
    	public static final Date getSecondSundayOfMarch () {
    		
    		Calendar cal = Calendar.getInstance();
    		cal.set(Calendar.MONTH, Calendar.MARCH);
    		cal.setFirstDayOfWeek(Calendar.SUNDAY);
    		cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    		cal.set(Calendar.WEEK_OF_MONTH,2);
    		return cal.getTime();
    	}
    	
    	public static final Date getFirstSundayOfNovember () {
    		Calendar cal = Calendar.getInstance();
    		cal.set(Calendar.MONTH, Calendar.NOVEMBER);
    		cal.setFirstDayOfWeek(Calendar.SUNDAY);
    		cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    		cal.set(Calendar.WEEK_OF_MONTH,1);
    		return cal.getTime();
    	}
    	
    }
    

    regards franz

    reward points if useful

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 14, 2007 at 09:21 AM

    Try This ..

    private static final String PST_2007_START_DST = "03/11/2007";

    private static final String PST_2007_END_DST = "11/04/2007";

    private static final String PST_TIMEZONE_LOCATION = "PST";

    private static final String DATE_FORMAT = "MM/dd/yyyy";

    private Date startDST;

    private Date endDST;

    private String startDSTString;

    private String endDSTString;

    private TimeZone timeZone;

    private String timezonLocation;

    public CheckDST(String timezonLocation, String entryDate, String exitDate)

    throws Exception {

    this.timezonLocation = timezonLocation;

    this.startDSTString = entryDate;;

    this.endDSTString = exitDate;

    if (!isValidTimeZone(timezonLocation)){

    throw new Exception("Not a valid Timezone: " + timezonLocation);

    }

    timeZone = TimeZone.getTimeZone(timezonLocation);

    startDST = this.stringToDate(entryDate, DATE_FORMAT);

    endDST = this.stringToDate(exitDate, DATE_FORMAT);

    }

    Then you main to implement..

    and the functions like

    public static boolean dstStartDateIsTransitioningProperly(TimeZone timezone, Date startDST){

    return (!timezone.inDaylightTime(previousDay(startDST)) &&

    timezone.inDaylightTime(dayAfter(startDST)));

    }

    public static boolean dstExitDateIsTransitioningProperly(TimeZone timezone, Date endDST){

    return (timezone.inDaylightTime(previousDay(endDST)) &&

    !timezone.inDaylightTime(dayAfter(endDST)));

    }

    public static boolean isValidTimeZone(String timezoneLocation) {

    String[] allTimeZoneIds = TimeZone.getAvailableIDs();

    boolean foundTimezone = false;

    for (int i = 0; i < allTimeZoneIds.length; i++) {

    if (timezoneLocation.equals(allTimeZoneIds<i>)) {

    foundTimezone = true;

    break;

    }

    }

    return foundTimezone;

    }

    public boolean entryDSTWorksProperly() {

    return dstStartDateIsTransitioningProperly(this.timeZone, this.startDST);

    }

    public boolean exitDSTWorksProperly() {

    return dstExitDateIsTransitioningProperly(this.timeZone, this.endDST);

    }

    private Date stringToDate(String dateStr, String dateFormat)

    throws java.text.ParseException {

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);

    return simpleDateFormat.parse(dateStr);

    }

    private static Date previousDay(Date date) {

    return new Date(date.getTime() - 1000 * 60 * 60 * 24);

    }

    private static Date dayAfter(Date date) {

    return new Date(date.getTime() + 1000 * 60 * 60 * 24);

    }

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 14, 2007 at 09:26 AM

    If you want to do more simply:

    public static final String[] DAYS = { "", "Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday" , "Wednesday", };

    public static String getDate(String sDate) {

    StringTokenizer st = new StringTokenizer(sDate, "-");

    int date = Integer.parseInt(st.nextToken());

    int month = Integer.parseInt(st.nextToken());

    int year = Integer.parseInt(st.nextToken());

    Calendar cal = Calendar.getInstance();

    cal.set(year, month, date);

    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

    return DAYS[dayOfWeek];

    }

    usage:

    ...

    Now you do a "while" and ask the later function until you fin the second sunday of march and the first sunday of november.

    Regards.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Nov 14, 2007 at 10:27 AM
    Add comment
    10|10000 characters needed characters exceeded