Reputation: 89
public static void getWeeksOfMonth(int month, int year)
{
SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd-MMM-yyyy");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, 1);
int ndays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println(ndays+"<<<ff" );
for (int i = 1; i <= ndays; i++)
{
String day = sdf.format(cal.getTime());
System.out.println(day+"<<<" );
Log.e("quest", day+"<<<");
if(i % 7 == 0){
Log.e("question", "=======week days===========");
}
cal.add(Calendar.DATE, 1);
}
}
Output
Saturday 04-Nov-2017
Sunday 05-Nov-2017
Monday 06-Nov-2017
Tuesday 07-Nov-2017
=======week days===========
Wednesday 08-Nov-2017
Thursday 09-Nov-2017
Friday 10-Nov-2017
Saturday 11-Nov-2017
Sunday 12-Nov-2017
Monday 13-Nov-2017
Tuesday 14-Nov-2017
=======week days==========
Wednesday 15-Nov-2017
Thursday 16-Nov-2017
Friday 17-Nov-2017
Saturday 18-Nov-2017
Sunday 19-Nov-2017
Monday 20-Nov-2017
Tuesday 21-Nov-2017
=======week days===========
Wednesday 22-Nov-2017
Thursday 23-Nov-2017
Friday 24-Nov-2017
Saturday 25-Nov-2017
Sunday 26-Nov-2017
Monday 27-Nov-2017
Tuesday 28-Nov-2017
=======week days===========
Wednesday 29-Nov-2017
Thursday 30-Nov-2017
1:-But i want complete last week Wed to Tuesday
2:-start weeks from Friday and end with Thursday
Upvotes: 0
Views: 188
Reputation: 338276
You are using terrible old classes, now supplanted by java.time classes.
The YearMonth
class represents an entire month.
YearMonth ym = YearMonth.of( 2018 , Month.JANUARY ) ;
Define your format for the output strings.
Locale locale = Locale.US ;
DateTimeFormatter f = DateTimeFormatter.ofPattern(
"EEEE dd-MMM-uuuu" ,
locale
) ;
I suggest instead letting java.time automatically localize for you rather than hard-code a format. To localize, specify:
FormatStyle
to determine how long or abbreviated should the string be.Locale
to determine:
Define formatter:
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate(
FormatStyle.LONG ,
Locale.CANADA_FRENCH
) ;
Collect into a List
perhaps.
int initialCapacity = ym.lengthOfMonth() ;
List< String > outputs = new ArrayList<>( initialCapacity ) ;
Cycle through the days.
for( int i = 1 , i <= ym.lengthOfMonth() ; i ++ ) {
LocalDate ld = ym.atDay( i ) ;
String output = ld.format( f ) ;
outputs.add( output ) ;
}
To group them by day-of-week, use the DayOfWeek
enum.
DayOfWeek dow = ld.getDayOfWeek() ;
if ( dow.equals( DayOfWeek.WEDNESDAY ) ) { … }
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
Upvotes: 1