month « Date Time « Java Data Type Q&A

1. Given a Date Object how do I determine the last day of its month?

I'm trying to use the following code but it's returning the wrong day of month.

Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, sampleDay.get(Calendar.MONTH)+1);
cal.set(Calendar.DAY_OF_MONTH, 0);
return cal.getTime();

2. Java Date month difference

I have start date and end date. I need the number of months between this two dates in java. For eg, from date:29-01-2009 to date :02-02-2009 (It ...

3. java.util.Date - Deleting three months from a date?

I have a date of type java.util.Date I want to subtract three months from it. Not finding a lot of joy in the API.

4. Date earlier than a month ago

How to check whether the given date is earlier than a month ago? What is the fastest algorithm? I have to take into account that different months have different numbers of ...

5. set DateTime to start of month

My API allows library client to pass Date:

method(java.util.Date date)
Working with joda-time, from this date I would like to extract the month and iterate over all days this month contains. Now, the passed ...

6. Convert month string to integer in java

Given a month string such as:

Is there any core java or third party library functionality that would allow you to convert this string ...

7. How to get short month names in Joda Time?

Does anyone know if there's a method in Joda Time or Java itself which takes either an int or a String as an argument, e.g. 4 or "4" and gives the ...

8. How can I find the most recent month end date with Joda time?

Say a month end date is the date in a month which is the last non-weekend and non-holiday day in that month. How can I find the most recent past ...

9. java example to get all weekend dates in a given month

I need to find all the weekend dates for a given month and year, eg: 01(month), 2010(year) . the output should be :- 2,3,9,10,16,17,23,24,30,31. all weekend dates. please any ...

10. Only one expression to get the Date of yesterday and the first day of month

Hi i am using a reporting tools which only support one line expression By example I want to get the Date of yesterday the class Calendar has a add method, but it returns ...

11. obtaining ammount of days of a given month with jodatime (Java)

30 days hath September,
   April, June and November,
 All the rest have 31,
   Excepting February alone
(And that has 28 days clear,
   With 29 in each ...

12. Increment date by 1 & loop until end of the month

i hav String date & i want to inceament date by 1 & it should be loop until end of the month. as examle, if i take November 2010 it should loop ...

13. How to detect if today or tomorrow is the 2nd or last Wed of the month in Java?

How to detect if today or tomorrow is the 2nd or last Wed of the month in Java ? Any sample code ?

14. Convert a string date with full month names to a date object in java

How can I convert dates with full month names into a Date object in Java? Here's a sample date that I need to convert: 6 December 2002. Thanks.

15. Invalid value 0 for Month field. while converting to XMLgregoriandate?

I am converting java.util.Date to XMLGregorianDate. When I print My util date on console, it displays:

Date: 2011-01-25 20:33:46.54
But after conversion, when I try to use it, gives me ...

16. i want boundry of any month

i am developing time attendance system in java, in that i want the duration of one month that means if we give only month then i want to display boundary of ...

17. Java get month string from integer

Is there a better way to compact this method i.e. reduce the cyclomatic complexity by avoid the switch cases?

String monthString;
        switch (month) {

18. SimpleDateFormatter does not recognize months

I want to parse a date string but i fail miserably. To illustrate my problem i wrote this simple junit test:

public void testParseJavaDate() throws ParseException {     

19. wrong month in date parsing

I have a string in the following format.

07/06/2011 5:06
mm/dd/yyyy hh:mm
am using following code
DateFormat dateFormater = new SimpleDateFormat("mm/dd/yyyy hh:mm");
date = (Date)dateFormater.parse(dateTimeStr);

but for input = 03/05/2011 9:45
i get op = Wed Jan ...

20. Want to get the start and end date of a month from a particular date

I m having a code which gives date of a particular event like :

String date=date1.substring(3,5)+"/"+date1.substring(0,2)+"/"+date1.substring(6,10);
I want the start date and end date of the month of which the above date belongs ...

21. Java: Get month Integer from Date

How do I get the month as an integer from a Date object (java.util.Date)?

22. How to build first and last of month as timestamp, with given timestamp?

Assuming a given Timestamp timestamp: I am using Joda Time to build the first day of the month:

DateTime dateTime = new DateTime(timestamp);    
MutableDateTime firstOfMonth = dateTime.toMutableDateTime();
firstOfMonth.setTime(0, 0, 0, 0);
and ...

23. dayOfMonth(31) in Month.AUGUST

Following code calculates the exact date when a child graduates from Kindergarden. Its always the 31th August after the 6th birthday. dob is the childĀ“s birthday :

public static DateMidnight getKigaAustritt(DateMidnight dob){

24. The month of date cannot be displayed properly in java selection list

Alex Fung When I use GotFree Cards ( to send free e-cards, I come across a problem when selecting the future date for sending the card. The webpage uses JavaScript and ...

25. the date of the last day of month

26. problem: How to get last month date

Hi all, quick question: I have a date (Date, 2006-05-12) and i would like to get the same date last month: 2005-04-12. right now I'm using: int future= -30 ; Calendar c = Calendar.getInstance(); c.setTime(someDay); c.add(Calendar.DATE, future); the problem is that a month can have 28,30,31 days, and as a result the previuos month will be incorrect. any idea?

27. how to get number of months between two given dates in java

If i remember, i used two calendars, one in the original date and one in the reference date. Then added while the original calendar was less (Date.before(Date)) than the reference date, have an integer variable holding how many cicles you have used. If not, add a month. Using the Calendar.add(). Hopes it helps.

28. Getting Last Date of Previous Month

29. Compare date cannot be greater than 12 months apart

Hmm, well I would parse the Date, put year, month and days into an integer each, then see if it's the same year or not. If it's the same year it's obviously not more then 12 months apart. If there is a two year difference it's obviously more then 12 months apart. If there is a one year difference you have ...

30. first date and last date of month

No offense, but I have to say that class is a mess. First of all, you use a lot of formatting + parsing to get rid of the day, just to set it afterwards. Second, your getLastDay method does not take into account daylight savings time, when one day can have 23 hours instead of 24. Thirdly, your if-statement in the ...

31. Date need to be advance to next month

Hi All, From a page user enters Start date and End date. A java program returns one month advance of both dates as "Start date || End date". e.g Suppose, Start Date = 04-06-2008, End Date = 10-06-2008 Then return value = 04-07-2008 || 10-07-2008 Now, a problem comes when I have select first day of month as start date and ...

32. Extracting months from Date object data

33. i want to get the month of a particular date

Calendar.getInstance() returns a Calendar object that represents the current date and time. You would need to use the set or setTime methods to set it to the date you want, but this would require you to know the month, which sort of defeats the purpose. In what form have you got the date that you want to know the month of ...

34. Incrementing Date By 6 Months

35. Getting months from date

Hi, You can do it using API. But i feel that it will be easy applying your own logic. For example parse the given dates and get the startyear,endyear,startmonth,endmonth. Now years=endyear-startyear; months=(years-2)*12; //since start year and end year are not completely included months=months+(endmonth-1)+(12-startmonth);//since start month and end months are not completely inlcuded. Correct me if i am wrong

37. how do I find date difference in months

I went through some of the examples,but I found dicrepencies in months difference package; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Hello { public Hello(){ } public static void main(String args[]) { int monthsDiff = monthsBetween(); System.out.println("Hello monthsDiff :"+monthsDiff); } public static int monthsBetween( ) { /* * Check out java.util.Calendar. * The difference in ...

38. want the first date and last date of each month

thanks for reply and what is your reply and what is my query i requested that any one knows the as per the example the date of a month with beginning and ending then the answer is or has to be like this for ( Calendar c = cal1; c.compareTo ( cal2 )<= 0; c.add ( Calendar.MONTH, 1 ) ) { ...

39. Java Date Class - Cannot get program to display 12 as Month - Help Needed

jverd, Thanks for your reply. My instructor wants us to use the Date class only. He is aware that the Date class only prints numbers 0 to 11 for the month, but he told us to add 1 so the months display properly...but I cannot figure out where to add 1 like he is saying. All other months 1 to 11 ...

40. Methods and declaring an int to a date objects Month

The main thing i am trying to do is after creating a date array with date objects, i wanna assign a integer value to each Month which could appear in the date object, so that when i come to search for the months, i can search for its integer value rather than the word.

41. subtract two dates to get months and days

43. get month from date

44. Get date minus 3 months

45. Date difference in months

/** * Compute the difference between two dates in years, months, and days. * The result is returned in a concatenated integer value of the form * ymmdd. So the difference between Apr 20, 2003 and Apr 15, 2025 would * be 211126 (21 years, 11 months, and 26 days) since 21 years and * 11 months gets you to May ...

46. Number of months between two dates

47. Date need to be advance to next month

Hi tschodt, Thanks for your suggestion. I have tried at my end and found, this logic won't work for February month. If I take Start date = 01-02-2008 and End date = 29-02-2008, it works fine and return 01-03-2008 || 31-03-2008. But, If I take Start date = 01-02-2008 and End date = 28-02-2008, it return 01-03-2008 || 30-03-2008, which is ...

49. Getting months betweem two given dates, result not in integer

Here's a general guideline: take the earlier date, parse it, then print it's month and year, then add one to it's month field, check if it's still earlier then the second date, and so on, so on, Use before method to find out wheather, you've reached the later date. Look for Calendar and Date methods. HTH Edited by: G.W. on Sep ...

51. Print dates for all mondays and fridays in a month?

Sure. One option is to iterate through the days of the month, for each day test whether it's a Monday or Friday, if it is, print in. A bit more complicated, test the weekday of the first of the month, determine how many days to add to get to the first Monday or Friday in the month. Every time you have ...

52. Date should not exceed more than 3 months

53. Getting no. of months by comparing two Dates

54. Checking for month boundaries given a date