I want to detect date of First Sunday/Monday of first/second week in every month in java? How can I achieve it?
I have checked Calendar class as well as Date class in java but not able to get solution for that so please help me out for this.
I want to detect date of First Sunday/Monday of first/second week in every month in java? How can I achieve it?
I have checked Calendar class as well as Date class in java but not able to get solution for that so please help me out for this.
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("MMM/dd/YYYY");
calendar.set(Calendar.MONTH,Calendar.JUNE);
calendar.set(Calendar.DAY_OF_MONTH,1);
int day = (Calendar.TUESDAY-calendar.get(Calendar.DAY_OF_WEEK));
if(day<0){
calendar.add(Calendar.DATE,7+(day));
}else{
calendar.add(Calendar.DATE,day);
}
System.out.println("First date is "+sdf.format(calendar.getTime()));`enter code here`
This will Give you First Saturday Of every month Of a Year of system date... u can also get other replace saturday with any other day
Invented By HK2 and Hemant
import java.util.Calendar;
import java.util.GregorianCalendar
class Demo{
SimpleDateFormat format =new SimpleDateFormat("dd-MMM-yyyy");
String DATE_FORMAT = "yyyy MM dd";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
Calendar c1 = Calendar.getInstance();
String y=sdf.format(c1.getTime());
String years=y.substring(0,4);
int year=Integer.parseInt(years);
Calendar cal = new GregorianCalendar(year, Calendar.JANUARY, 1);
for (int i = 0, inc = 1; i <366 && cal.get(Calendar.YEAR) == year; i+=inc) {
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
String frm="";
frm=format.format(cal.getTime());
// System.out.println("the value of the saturday is "+format.format(cal.getTime()));
cal.add(Calendar.MONTH,1);
cal.set(Calendar.DAY_OF_MONTH, 1);
}
else {
cal.add(Calendar.DAY_OF_MONTH, 1);
}
}
}
Using java.time
library built into Java 8:
import java.time.{DayOfWeek, LocalDate}
import java.time.temporal.TemporalAdjusters.firstInMonth
val now = LocalDate.now() # 2015-11-23
val firstMonday = now.`with`(firstInMonth(DayOfWeek.MONDAY)) # 2015-11-02 (Monday)
You may choose any day from java.time.DayOfWeek.{MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY}
.
If you need to add time information, you may use any available LocalDate
to LocalDateTime
conversion like
firstMonday.atStartOfDay() # 2015-11-02T00:00
The simplest method of doing it with Java 8 is:
LocalDate firstSundayOfNextMonth = LocalDate
.now()
.with(firstDayOfNextMonth())
.with(nextOrSame(DayOfWeek.MONDAY));
You can get the day of the week for a specific date in Java using Calendar
class as discussed here:
How to determine day of week by passing specific date?
You can then loop over the dates in your range to find the first instance of Sunday and the second instance of Monday.
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, date);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
So it'll give day for month first date. From this you can calculate what you want.
public static Calendar getFirstMonday(Date startDate) {
Calendar c = Calendar.getInstance();
c.setTime(startDate);
int day = c.get(Calendar.DAY_OF_WEEK);
while (day != 1) {
c.add(Calendar.DATE, 1);
day = c.get(Calendar.DAY_OF_WEEK);
}
return c;
}
Find fisr monday of all weeks by:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String strDate = "01/01/2014";
Date date;
try {
date = sdf.parse(strDate);
for (int i = 0; i < 52; i++) {
Calendar c = getFirstMonday(date);
System.out.println("monday: " + sdf.format(c.getTime()));
c.add(Calendar.DATE, 7);
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}