1

I am trying to calculate week numbers where the start of the week number must start with February first week. For example. Feb 2-feb 8 2015 the week number must be 1. Please help me as I am new to this environment and I am able to calculate the week numbers starting with January first week.

To add more context I am trying to implement a Broadcast calendar in java with week 1 starting from February.

  • 1
    if you could try to be a bit more clear what you're looking for, that would be helpful. I don't understand your example. So would the 9-15 be week 2, etc. etc.? and why do you say must start with February first week, then switch to January? Which is it? – tlw11591 Feb 11 '15 at 13:21
  • Yea 9-15 must be second week . I am using Calendar.WEEK_OF_YEAR to find out the week number currently – Siva Prasad Feb 11 '15 at 13:25

2 Answers2

1

java.time

The LocalDate class represents a date-only value without time-of-day and without time zone.

Specify your desired date as an anchor date, a date from which we count weeks. You want ordinal week numbers, so add one to avoid a week numbered zero.

The ChronoUnit enum offers the between method to calculate elapsed time.

LocalDate anchor = LocalDate.of( 2015 , Month.FEBRUARY , 2 );

LocalDate feb5 = LocalDate.of( 2015 , Month.FEBRUARY , 5 );
LocalDate feb8 = LocalDate.of( 2015 , Month.FEBRUARY , 8 );
LocalDate feb9 = LocalDate.of( 2015 , Month.FEBRUARY , 9 );

System.out.println( feb5 + " is in week # " + ( ChronoUnit.WEEKS.between( anchor , feb5 ) + 1 ) );
System.out.println( feb8 + " is in week # " + ( ChronoUnit.WEEKS.between( anchor , feb8 ) + 1 ) );
System.out.println( feb9 + " is in week # " + ( ChronoUnit.WEEKS.between( anchor , feb9 ) + 1 ) );

See this code run live at IdeOne.com.

2015-02-05 is in week # 1

2015-02-08 is in week # 1

2015-02-09 is in week # 2


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?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

So just use

Calendar c = Calendar.getInstance();
DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
c.setTime( sdf.parse("2/2/2015"));
out.println( c.get( Calendar.WEEK_OF_YEAR ));

to get the week number for February 2, and offset it by the number of weeks between Jan. 1st. So

c.get( Calendar.WEEK_OF_YEAR )) - 4; 

or however many weeks are in front of it.

tlw11591
  • 330
  • 1
  • 3
  • 6