0

i want to do DATENAME(q, getDate()) the same in Java. this command works fine in sql server, i want to replicate the same process in java

i tried

Integer quater = (new LocalDate().getMonthOfYear()/3)+1;
System.out.println(quater);

Expected Result : 4
obtain Result : 4

Integer quater = (new LocalDate(2013,9,10).getMonthOfYear()/3)+1;
System.out.println(quater);

Expected Result : 3 
obtain Result : 4

please let me know exact command to get this.

Sliq
  • 15,937
  • 27
  • 110
  • 143
buttowski
  • 4,657
  • 8
  • 27
  • 33
  • `9/3 +1 = 4` so the given output makes mathematical sense. Where did you get your logic from? Try using `/4` instead of `/3` – Amber Oct 08 '13 at 11:31
  • yes i just want find formula behind DATENAME(q, getDate()), and i know my calculation returns 3. but i need formula to get result as 4. or is this already available in any existing library. – buttowski Oct 08 '13 at 11:39
  • possible duplicate of [How do I discover the Quarter of a given Date?](http://stackoverflow.com/questions/302658/how-do-i-discover-the-quarter-of-a-given-date) – GarethD Oct 08 '13 at 12:50
  • Further more I think `DATEPART(QUARTER` is as simple as `CEILING(MonthNumber / 3)`. http://sqlfiddle.com/#!3/d41d8/22442 – GarethD Oct 08 '13 at 12:53

1 Answers1

1

Please try

Integer quarter = (int) Math.ceil((new LocalDate(2013,9,10).getMonthOfYear() - 1) / 3) + 1;
Szymon
  • 42,577
  • 16
  • 96
  • 114