CeccoCQ
CeccoCQ

Reputation: 3754

Java get half month number

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?

Upvotes: 2

Views: 2684

Answers (4)

Basil Bourque
Basil Bourque

Reputation: 340108

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.

Upvotes: 0

jarrad
jarrad

Reputation: 3292

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 );

Upvotes: 2

Alderath
Alderath

Reputation: 3874

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).

Upvotes: 0

Snicolas
Snicolas

Reputation: 38168

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.

Upvotes: 0

Related Questions