Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleDateformat and "Day of week in month" (F)

Tags:

java

I would like to get which day of the week is the current day and looking the SimpleDateFormat class I tought that the "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 output. Instead I get 2.

As english isn't my mothertongue, did I missunderstand the meaning of the format?

like image 705
Francesco Avatar asked Jan 09 '13 09:01

Francesco


People also ask

What is the format of SimpleDateFormat?

Class SimpleDateFormat. SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.

What is the difference between DateFormat and SimpleDateFormat?

The java SimpleDateFormat allows construction of arbitrary non-localized formats. The java DateFormat allows construction of three localized formats each for dates and times, via its factory methods.

How do I change date format in SimpleDateFormat?

You can just use: Date yourDate = new Date(); SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); String date = DATE_FORMAT. format(yourDate);

What is the difference between DateTimeFormatter and SimpleDateFormat?

DateTimeFormatter is a replacement for the old SimpleDateFormat that is thread-safe and provides additional functionality.


3 Answers

F - Day of week in month

E - Day name in week

try u - Day number of week (1 = Monday, ..., 7 = Sunday)

Note that 'u' is since Java 7, but if you need just day number of the week then use Calendar

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

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

    c.setFirstDayOfWeek(Calendar.SUNDAY);
like image 114
Evgeniy Dorofeev Avatar answered Sep 17 '22 19:09

Evgeniy Dorofeev


Today is the second Wednesday in the current month.

like image 32
kan Avatar answered Sep 19 '22 19:09

kan


F = Day of Week in Month
1day to 7day, it will print 1.
8day to 14day, it will print 2.
15day to 21day, it will print 3.
22day to 28day, it will print 4 and
29day to 31day, it will print 5.

like image 44
Zaw Htet Aung Avatar answered Sep 17 '22 19:09

Zaw Htet Aung