2

I would to get the number of the half month od the year starting from a date. For example, I have 13-Mar-2012, and I have 6 as result.

I've tried with Calendar class, but doesn't work properly:

    Calendar cal = (GregorianCalendar) Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH,13);
    cal.set(Calendar.MONTH, 2);
    cal.set(Calendar.YEAR, 2012);
    int weekNum = cal.get(Calendar.WEEK_OF_YEAR);
    System.out.println("Weeknum:" + ((weekNum/2)));

Can anyone help me?

Joe Doyle
  • 6,363
  • 3
  • 42
  • 45
CeccoCQ
  • 3,746
  • 12
  • 50
  • 81

4 Answers4

2

Assuming Half month as defined here: http://en.wikipedia.org/wiki/Half-month

  Calendar cal = (GregorianCalendar) Calendar.getInstance();
  cal.set(Calendar.DAY_OF_MONTH, 13);
  cal.set(Calendar.MONTH, 2);
  cal.set(Calendar.YEAR, 2012);
  // remember, we have a zero based month
  int halfMonth = cal.get( Calendar.MONTH ) * 2 + 1;
  // 1-15 is first half-month 16-end of month is second
  int remainder = cal.get( Calendar.DAY_OF_MONTH ) / 16; 
  halfMonth += remainder;

  System.out.println( halfMonth );
jarrad
  • 3,292
  • 1
  • 23
  • 15
  • That code is a bit weird. You should divide by 2, not by the number of the current month. This happens to work because the example date lies in february, but for any other month it would fail. – Alderath May 02 '12 at 11:46
  • My bad. Simply assumed that MONTH would be 1-based, since both DAY_OF_YEAR and WEEK_OF_YEAR were 1-based. I wonder what the reason for mixing 0-based and 1-based variables in the same API is... – Alderath May 02 '12 at 14:48
0
Calendar cal = (GregorianCalendar) Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH,13);
cal.set(Calendar.MONTH, 2);
cal.set(Calendar.YEAR, 2012);
int hafMonthCount = cal.get(Calendar.DAY_OF_YEAR) / 14 ;
//here you must multiply by 2 :)
System.out.println("HalfMonthCount:" + hafMonthCount );

---updated

As the concept you use is not implemented in Java (in french we have this concept of quizaine for 14 days but in english I can't say), you must compute it by yourself.

Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • Ok, but I still have 5 as result. – CeccoCQ Apr 27 '12 at 12:52
  • 1
    You have to add 1 to hfMonthCount. – CeccoCQ Apr 27 '12 at 12:57
  • Or maybe use DAY_OF_WEEK to adjust the value. – Snicolas Apr 27 '12 at 13:24
  • This solution differs from what it seems (to me) like he is trying to achieve. If I understood it, he wants the result to be 1 for hfMonthCount when WEEK_OF_YEAR is 1 or 2. And 2 when WEEK_OF_YEAR is 3 or 4. Your solution will not do this. Your solution will only work when the first day of the year is a monday (or whatever day the week starts with in your locale). – Alderath May 02 '12 at 11:32
  • Example of what I said in previous comment: Assume: Weeks start with mondays in your locale and the first day of this year is a thursday. If using your solution, day 13 (of the year) would be part of the first "half month". However day 13 is a tuesday, and hence the 3:d week has started, and the "half month" number should be 2. – Alderath May 02 '12 at 11:39
  • And btw, two weeks is in English sometimes called a [fortnight](http://en.wikipedia.org/wiki/Fortnight). I'm not sure, as I am not a native English speaker, but maybe the expression fortnight is a bit old-fashioned. – Alderath May 02 '12 at 11:40
  • And I just realized that there is an additional error, equivalent to the error made by the OP, which raised this question. When DAY_OF_YEAR is 1, the hfMonthCount will be 0. Not 1, as it is supposed to be. (Making my previous comments a bit incorrect, but the concept they describe is still there). – Alderath May 02 '12 at 11:50
0

Details to show by example what happens with your code. Assume we have the following four different values of WEEK_OF_YEAR:

WEEK_OF_YEAR: 1
WEEK_OF_YEAR: 2
WEEK_OF_YEAR: 3
WEEK_OF_YEAR: 4

What will happen if we divide these values by 2?

WEEK_OF_YEAR: 1          (weekNum/2) = 1/2 = 0 
WEEK_OF_YEAR: 2          (weekNum/2) = 2/2 = 1
WEEK_OF_YEAR: 3          (weekNum/2) = 3/2 = 1
WEEK_OF_YEAR: 4          (weekNum/2) = 4/2 = 2

So the issue with your code is that it will result in the first week of the year resulting in a value 0. So what you'd want to be doing in your code is to replace the (weekNum/2) with ((weekNum + 1)/2).

Alderath
  • 3,761
  • 1
  • 25
  • 43
0

If the astronomy Half-Month is intended (not to be confused with an astronomy fortnight), then the Answer by jarrad is correct. But we have more modern classes at our disposal now, the java.time classes.

enter image description here

LocalDate

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

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate ld = LocalDate.now( z );

Get the month number, 1-12 for January-December.

int monthNumber = ld.getMonthValue();  // 1-12.

Multiply that month number by two, as there are two month-halves in every month. If early in the month, subtract one (so 6 becomes 5, for example).

int adjustment = ( ld.getDayOfMonth() < 16 ) ? 1 : 0 ;  // If first half of month, back off the half-month-number by 1. 
int halfMonthNumber = ( ( monthNumber * 2 ) - adjustment ); // 1-24.

The astronomy Half-Month labels each with a English letter, A-Y while omitting I. So we extract a letter from this subset alphabet of 24 letters by the half-month-number.

int index = ( halfMonthNumber - 1 );  // Subtract one for zero-based counting.
String alphaCode = "ABCDEFGHJKLMNOPQRSTUVWXY".substring( index , index + 1 );

I have not run that code, just typed off the top of my head. Use at your own risk, and please fix if needed.

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

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.

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