Михаель
Зарегистрирован: 22.05.2026 Сообщения: 5846
|
Добавлено: Чт Ноя 12, 2026 10:45 pm Заголовок сообщения: (!!Flirt!!^) how to find out the day of a particular date |
|
|
Hello, Guest!
Article about how to find out the day of a particular date:
Additionally
if just the day ordinal is desired
how can that be retrieved? If you're reading this
please skip ahead/down to the answers using the current methodology of java.time! The Calendar-based answers are fine
but outdated now.
Click here for How to find out the day of a particular date
29 Answers 29. Yes. Depending on your exact case: You can use java.util.Calendar : if you need the output to be Tue rather than 3 (Days of week are indexed starting at 1 for Sunday
see Calendar.SUNDAY)
instead of going through a calendar
just reformat the string: new SimpleDateFormat("EE").format(date) ( EE meaning "day of week
short version") if you have your input as string
rather than Date
you should use SimpleDateFormat to parse it: new SimpleDateFormat("dd/M/yyyy").parse(dateString) you can use joda-time's DateTime and call dateTime.dayOfWeek() and/or DateTimeFormat . edit: since Java 8 you can now use java.time package instead of joda-time. In other things to be careful of
if you set the date in the Calendar object using integers (not via parsing a string)
then be aware that the month number is zero-based
so January is 0 and December is 11. @RenniePet: Good one. Also you can use constants in Calendar class
such as Calendar.SUNDAY or Calendar.JANUARY . This answer was a great answer when it was written. Today you will want to the now outdated classes Calendar
Date and DateTimeFormat (and Joda time too) and use the Java 8 java.time classes
for instance YearMonth or LocalDate . See more in this answer. FYI
the troublesome old date-time classes such as java.util.Date
java.util.Calendar
and java.text.SimpleDateFormat are now legacy
supplanted by the java.time classes built into Java 8 & Java 9. See Tutorial by Oracle. Use this code for find the day name from a input date.Simple and well tested. FYI
the troublesome old date-time classes such as java.util.Date
java.util.Calendar
and java.text.SimpleDateFormat are now legacy
supplanted by the java.time classes built into Java 8 & Java 9. See Tutorial by Oracle. The result is: Sat
28 Dec 2013. The default constructor is taking "the default" Locale
so be careful using it when you need a specific pattern. See this code run live at IdeOne.com (but only Locale.US works there). java.time. See my example code above
and see the correct Answer for java.time by Przemek. Ordinal number. if just the day ordinal is desired
how can that be retrieved? For ordinal number
consider passing around the DayOfWeek enum object instead such as DayOfWeek.TUESDAY . Keep in mind that a DayOfWeek is a smart object
not just a string or mere integer number. Using those enum objects makes your code more self-documenting
ensures valid values
and provides type-safety. But if you insist
ask DayOfWeek for a number. You get 1-7 for Monday-Sunday per the ISO 8601 standard. Joda-Time. UPDATE: The Joda-Time project is now in maintenance mode. The team advises migrating to the java.time classes. The java.time framework is built into Java 8 (as well as back-ported to Java 6 & 7 and further adapted to Android). Here is example code using the Joda-Time library version 2.4
as mentioned in the accepted answer by Bozho. Joda-Time is far superior to the java.util.Date/.Calendar classes bundled with Java. LocalDate. Joda-Time offers the LocalDate class to represent a date-only without any time-of-day or time zone. Just what this Question calls for. The old java.util.Date/.Calendar classes bundled with Java lack this concept. Parse. Parse the string into a date value. Extract. Extract from the date value the day of week number and name. Dump to console. About java.time. 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? Java SE 9
and later Built-in. Part of the standard Java API with a bundled implementation. Java 9 adds some minor features and fixes. Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport . The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically. See How to use ThreeTenABP… . The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time.
how to find out the day of a particular date |
|