SimpleDateformat and "Day of the Week in the Month" (F)

I would like to get which day of the week is the current day, and looking at the SimpleDateFormat I class, that "F" is what I need. So I wrote a little test:

System.out.println(new SimpleDateFormat("F").format(new Date())); 

Today is Wednesday and I expect to get 3 as a way out. Instead, I get 2.

Since English is not my native language, did I miss an understanding of the meaning of the format?

+7
source share
6 answers

F - Day of the week per month

E - Day / Week Name

try u - Day of the week (1 = monday, ..., 7 = sunday)

Note that ā€œuā€ is Java 7, but if you only need the day of the week, use Calendar

  Calendar c = Calendar.getInstance(); System.out.println(c.get(Calendar.DAY_OF_WEEK)); 

You can change the first day of the week by changing Locale or directly as

  c.setFirstDayOfWeek(Calendar.SUNDAY); 
+19
source

Today is the second Wednesday of the current month.

+12
source

Indexes for the days of the week start at 0, not 1.

+1
source
 F -> Day of week in month(1-5) 

Today is 09/01/2013(dd/MM/yyyy) , which falls in the 2nd week, so it printed 2.

If you try using 16/01/2013 then it will print 3.

+1
source

java.util.Calendar / .Date and its related classes are confusing as you have learned the hard way. Counting from scratch for the month numbers and the number of days of the week is one of many bad design options made in these old classes.

java.time

Those old classes were supplanted in Java 8, and then java.time . The new classes are inspired by the highly successful Joda-Time framework, intended as its successor, similar in concept but with re-architecture. Defined by JSR 310 . Expanded by the ThreeTen-Extra project. See Tutorial .

DayOfWeek

During the day, use the well-named DayOfWeek enum .

If you want the weekly integer to be compatible with ISO 8601 , where Monday = 1 and Sunday = 7, you can extract this from an instance of DayOfWeek .

Conversion

If you start with the java.util.Calendar object, convert it to java.time.

An Instant is a moment on the timeline in UTC .

 Instant instant = myJavaUtilCalendarObject.toInstant(); 

Apply time zone to get the date, to get the day of the week.

 ZoneId zoneId = ZoneId.of( "America/Montreal" ); ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId ); DayOfWeek dayOfWeek = zdt.getDayOfWeek(); 

Please note that the time zone is crucial for determining the date and therefore the day of the week. The date is not the same all over the world at the same time. For example, in Paris, a new day appears earlier than in Montreal, for example.

The number of days of the week

Now that we have java.time and this listing is built into Java, I suggest that you freely pass those instances of enum, not the magic number .

But if you insist on integers, ask for DayOfWeek .

 int dayOfWeekNumber = dayOfWeek.getValue(); 

Day string

This enumeration generates a localized string for the name of the day of the week.

 String output = dayOfWeek.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ); // Or Locale.ENGLISH. 
+1
source

just use this method for this.

 public String day(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("EEEEEEEEE", new Locale("tr", "TR")); return sdf.format(date); } 
0
source

All Articles