how can I get a week of the year given a date? I tried the following code:
Calendar sDateCalendar = new GregorianCalendar();
sDateCalendar.set(Integer.parseInt(sDateYearAAAA), Integer.parseInt(sDateMonthMM)-1, Integer.parseInt(sDateDayDD));
System.out.format("sDateCalendar %tc\n", sDateCalendar);
iStartWeek = sDateCalendar.getWeekYear();
System.out.println("iStartWeek "+iStartWeek+ " "+sDateCalendar.WEEK_OF_YEAR);
and i obtain: sDateCalendar lun apr 23 11:58:39 CEST 2012 iStartWeek 2012 3
while the correct week of year is 17. Can someone help me ?
First, we extract the day as a number using java.util.Calendar: public static int getDayNumberOld(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal.get(Calendar.DAY_OF_WEEK); } The resulting number ranges from 1 (Sunday) to 7 (Saturday).
There are many ways to get a year from date of which frequently used two methods are listed below. Let us discuss each of them in detail alongside implementing the methods The get () method of LocalDate class in Java method gets the value of the specified field from this Date as an int.
For a year-week defined by the ISO 8601standard as starting on a Monday and first week contains the first Thursday of the calendar year, use the YearWeekclass from the ThreeTen-Extralibrary that adds functionality to the java.timeclasses built into Java.
Method 2: Using Calendar class in Java: The idea is to use get () method of Calendar class to get the day, month, and year from the date. The get () method takes one parameter of integer type and returns the value of the passed field from the given date. It returns the month index instead of the month name.
For a year-week defined by the ISO 8601 standard as starting on a Monday and first week contains the first Thursday of the calendar year, use the YearWeek
class from the ThreeTen-Extra library that adds functionality to the java.time classes built into Java.
org.threeten.extra.YearWeek
.from(
LocalDate.of( 2012 , Month.APRIL , 23 )
)
.toString()
2012-W17
You need to define week-of-year.
Beware that the old java.util.Calendar class has a definition of week that varies by Locale.
For many reasons, you should avoid the old java.util.Date/.Calendar classes. Instead use the new java.time framework.
Java 8 and later comes with the java.time framework. Inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project. See Tutorial.
Here is some example code to getting the ISO 8601 standard week.
Getting a date, and therefore a week, depends on the time zone.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime now = ZonedDateTime.now( zoneId );
The IsoFields
class defines a week-based year. We can ask for the:
WEEK_OF_WEEK_BASED_YEAR
)WEEK_BASED_YEAR
).First we get the current date-time.
ZoneId zoneId = ZoneId.of ( "America/Montreal" );
ZonedDateTime now = ZonedDateTime.now ( zoneId );
Interrogate that date-time object, asking about the standard week-based year.
int week = now.get ( IsoFields.WEEK_OF_WEEK_BASED_YEAR );
int weekYear = now.get ( IsoFields.WEEK_BASED_YEAR );
Dump to console.
System.out.println ( "now: " + now + " is week: " + week + " of weekYear: " + weekYear );
now: 2016-01-17T20:55:27.263-05:00[America/Montreal] is week: 2 of weekYear: 2016
For more info, see this similar Question: How to calculate Date from ISO8601 week number in Java
WeekFields
In java.time you can also call upon the WeekFields
class, such as WeekFields.ISO.weekBasedYear()
. Should have the same effect as IsoFields
in later versions of Java 8 or later (some bugs were fixed in earlier versions of Java 8).
YearWeek
For standard ISO 8601 weeks, consider adding the ThreeTen-Extra library to your project to use the YearWeek
class.
YearWeek yw = YearWeek.of( 2012 , 17 ) ;
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
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes. Hibernate 5 & JPA 2.2 support java.time.
Where to obtain the java.time classes?
elegant way (no need for java.util.Calendar):
new SimpleDateFormat("w").format(new java.util.Date())
You are using sDateCalendar.WEEK_OF_YEAR, which is the static integer WEEK_OF_YEAR, see the source of the java.util.Calendar class:
public final static int WEEK_OF_YEAR = 3;
To get the week number, you should be using:
sDateCalendar.get(Calendar.WEEK_OF_YEAR);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With