How to get first day of week, when we know one of the date in the week?
I know the current month , the current year and the current date of week. Then, are there so
hello if you need to get the first day of the current week "monday"
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, Calendar.MONDAY-calendar.get(Calendar.DAY_OF_WEEK));
Log.w(TAG, "first day of week" + calendar.getTime());
2 if you need the first day of the week for 20-12-2016
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 20);
calendar.set(Calendar.MONTH,Calendar.DECEMBER);
calendar.set(Calendar.YEAR,2016);
calendar.add(Calendar.DAY_OF_MONTH,Calendar.MONDAY - calendar.get(Calendar.DAY_OF_WEEK));
Log.w(TAG,"first day of week:"+calendar.getTime());
i hope this will help you
hello if you need to get the first day of the current week "monday"
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, Calendar.MONDAY-calendar.get(Calendar.DAY_OF_WEEK));
Log.w(TAG, "first day of week" + calendar.getTime());
2 if you need the first day of the week for 20-12-2016
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 20);
calendar.set(Calendar.MONTH,Calendar.DECEMBER);
calendar.set(Calendar.YEAR,2016);
calendar.add(Calendar.DAY_OF_MONTH,Calendar.MONDAY - calendar.get(Calendar.DAY_OF_WEEK));
Log.w(TAG,"first day of week:"+calendar.getTime());
i hope this will help you
Try this:
// get today and clear time of day
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0); // ! clear would not reset the hour of day !
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);
// get start of this week in milliseconds
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
System.out.println("Start of this week: " + cal.getTime());
System.out.println("... in milliseconds: " + cal.getTimeInMillis());
Reference :How to get the first day of the current week and month?
Hope this may help you!
The modern approach uses the java.time classes rather than the troublesome old Calendar
& Date
classes.
LocalDate.of( 2017 , Month.MARCH , 23 )
.with( TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ) )
LocalDate
The LocalDate class represents a date-only value without time-of-day and without time zone.
LocalDate someDate = LocalDate.of( 2017 , Month.MARCH , 23 ) ;
TemporalAdjuster
To get the first day of the week, (a) decide what is the first day of the week for you, (b) use a TemporalAdjuster implementation defined in TemporalAdjusters to get the date for a specific DayOfWeek
enum object.
LocalDate ld = someDate.with( TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ) ) ;
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.
Where to obtain the java.time classes?