How to set SimpleDateFormat "W" to use MONDAY - is this the first day of the week?

SimpleDateFormat "W" defaults to SUNDAY - the first day of the week, for example:

SimpleDateFormat f = new SimpleDateFormat("W"); 
format.f(new Date()); 

I want to set "W" the first day on MONDAY, I try:

SimpleDateFormat f = new SimpleDateFormat("W"); 
Calendar c=Calendar.getInstance(); 
c.setTimeInMillis(input); 
c.setFirstDayOfWeek(Calendar.MONDAY); 
format.format(c.getTime()) 

but no effect. help me, thanks!

+3
source share
1 answer

use setCalendar () in your SimpleDateFormat to set the calendar used to calculate the date:

SimpleDateFormat format = new SimpleDateFormat("W");
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek( Calendar.MONDAY );
format.setCalendar( calendar ); 
format.format( whateverDateYouWantToFormat );`
+11
source

Source: https://habr.com/ru/post/1716424/


All Articles