473,323 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

How to find the total number of days in the month using LocalDateTime in Java 8?

I am using java 8 date and time classes in my code. I can find year, month, day, hours, minutes and seconds, but I cannot find a method to find a number of days in a month.

Here is my code.
Expand|Select|Wrap|Line Numbers
  1. String oldestDateString = "2015-10-01 00:00:00";
  2. String latestDateString = "2015-12-25 00:00:00";
  3.  
  4. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  5. LocalDateTime oldestDate = LocalDateTime.parse(oldestDateString, formatter);
  6. LocalDateTime latestDate = LocalDateTime.parse(latestDateString, formatter);
  7.  
  8. int year = oldestDate.getYear();
  9. int month = oldestDate.getMonthValue();
  10. int day = oldestDate.getDayOfMonth();
  11. int hours = oldestDate.getHour();
  12. int minutes = oldestDate.getMinute();
  13. int seconds = oldestDate.getSecond();
I also want to iterate through “oldestDate” to “latestDate”. How can I iterate through “oldestDate” to “latestDate” until the “oldestDate” reaches to “latestDate”?
Oct 15 '15 #1
1 1330
chaarmann
785 Expert 512MB
Well, I am not up-to-date with the latest java date-time libraries, as I am with GregorianCalendar and Joda-Time, but I can tell you a general trick that works independent of any library.
Number of days in February 2000:
  1. get date object for 1st March 2000.
  2. subtract one day.
  3. get the day of the resulting date.

About your second question:
iterate by using a for-loop starting with oldestDate and add one day in each step until you reach your latestDate.
Oct 21 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: John | last post by:
hey all..... alright, I am frusterated to the point of throwing my machine out the window (this board went down, trying to find stuff on google, this has been a nightmare) so I hope you guys can...
7
by: Egor Shipovalov | last post by:
I'm implementing paging through search results using cursors. Is there a better way to know total number of rows under a cursor than running a separate COUNT(*) query? I think PostgreSQL is bound...
2
by: project | last post by:
anybody can tell me how to find how many days in a month by using sql quiery Posted Via Usenet.com Premium Usenet Newsgroup Services ----------------------------------------------------------...
1
by: meryline | last post by:
hi, iam new to c#, can any one tell me how to find the total number characters entered in a textbox. like in vb.net i find it using len(textbox.text) . iam trying to implement the same in c# sharp...
2
by: karimufeed | last post by:
I am working on an access project for pension calculation. I want to find total length of service between two dates. i.e. if the Date of entry into service is 15/3/1980 and the date of retirement...
3
by: tasawer | last post by:
Hi, I need help to create an sql command to find no of days in the current month of calculation and no of days in the month previous to it. This code works for the current month, but only...
1
by: mixupatel | last post by:
create or replace function fun_getnoofrows(p_empcode number,p1_date varchar2,p2_date varchar2) return number as v_cnt number ; i number:=0; days number:=0; begin select ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.