cancel
Showing results for 
Search instead for 
Did you mean: 

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

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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

Answers (3)

Answers (3)

Former Member
0 Kudos
iaki_vila
Active Contributor
0 Kudos

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.

Former Member
0 Kudos

Hi

Thank you for the code. But can u give me the complete code, as I dont have any idea to use date classes.

aashish_sinha
Active Contributor
0 Kudos

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);

}