Solved

How to i get the last date of the last month

Posted on 2006-10-31
21
6,248 Views
Last Modified: 2012-05-05
I need to know how to i get the last date of the last month.

For example,

today is 01-11-2006

What i need is to get the last date of the last month

31-10-2006  

I try to use calendar but not working..

Please help
0
Comment
Question by:LeanMoreTryMore
  • 11
  • 5
  • 4
  • +1
21 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 17845098
Calendar c = Calendar.getInstance(); // (The 1st)
c.add(Calendar.DATE, -1);
System.out.println(c.getTime());
0
 

Author Comment

by:LeanMoreTryMore
ID: 17845123
very thanks for the quick response.

I need to convert to the format dd-mm-yyyy
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17845134
Just use a DateFormat

DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
System.out.println(df.format(c.getTime()));
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 9

Assisted Solution

by:shinobun
shinobun earned 100 total points
ID: 17845135
Another way is to call Calendar#add(Calendar.MONTH, -1) to get the last month, and Calendar#getActualMaximum() [1] on Calendar.DAY_OF_MONTH to get the last day.

Calendar c = ...;
c.add(Calendar.MONTH, -1);
c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.MONTH));

[1] http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html#getActualMaximum(int)
0
 

Author Comment

by:LeanMoreTryMore
ID: 17845138
Would you please explain to me which syntax to get the last date

i guess

calendar.date - 1  <== get the last month of the calendar date

0
 

Author Comment

by:LeanMoreTryMore
ID: 17845159
any easy way to get :

the first date of the month

actually my requirement is to get

. the first date of the month
. the last date of the month

0
 

Author Comment

by:LeanMoreTryMore
ID: 17845203
I dont understand

c.add(Calendar.DATE, -1);  <== i thought this one is to get the last month.

if I use the following syntax how to i get the first date of the last month

        Class1 class1 = new Class1();
        Calendar c = Calendar.getInstance(); // (The 1st)
        c.add(Calendar.DATE, -1);
        DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        System.out.println(df.format(c.getTime()));
0
 

Author Comment

by:LeanMoreTryMore
ID: 17845223
Hi shinobun

If i use the below, i get very strange result

        Calendar c = Calendar.getInstance(); // (The 1st)
        c.add(Calendar.MONTH, -1);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.MONTH));
        System.out.println(c.getTime());

Return result:

Wed Oct 11 07:46:30 EST 2006
0
 
LVL 92

Expert Comment

by:objects
ID: 17845236
expanding on shinobun's suggestion:

cal = Calendar.getInstance();
cal.setTime(date);
// first day of month
c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
Date first = c.getTime();
// last day of month
c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
Date last = c.getTime();
0
 
LVL 92

Expert Comment

by:objects
ID: 17845251
> c.add(Calendar.DATE, -1);  <== i thought this one is to get the last month.

thats wrong, to get the last month use:

c.add(Calendar.MONTH, -1);
0
 

Author Comment

by:LeanMoreTryMore
ID: 17845306
c.add(Calendar.DATE, -1);  <== i thought this one is to get the last month.
thats wrong, to get the last month use:

where to get the last date of the month


Object:
I got error:

        Date date = null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // first day of month
        c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
        Date first = c.getTime();
        System.out.println(c.getTime());
       
        // last day of month
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date last = c.getTime();
        System.out.println(c.getTime());

java.lang.NullPointerException

Please verify where i get wrong. thanks
0
 

Author Comment

by:LeanMoreTryMore
ID: 17845338
Object

I change to this, BUT I only get the first date of the current month and the last date of the current month:
Return result using the following
01-11-2006
30-11-2006


=================
        DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // first day of month
        c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
        Date first = c.getTime();
        System.out.println(df.format(c.getTime()));
       
        // last day of month
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date last = c.getTime();
        System.out.println(df.format(c.getTime()));


0
 

Author Comment

by:LeanMoreTryMore
ID: 17845370
Even i add

cal.add(Calendar.MONTH,-1);
c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));


I think i try before but it never works. obviously this is my limit knowledge in java. The following still returns the first date of the current month and the last date of the current month

        DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH,-1);
        cal.setTime(date);
        // first day of month
        c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
        Date first = c.getTime();
        System.out.println(df.format(c.getTime()));
       
        // last day of month
        cal.add(Calendar.MONTH,-1);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date last = c.getTime();
        System.out.println(df.format(c.getTime()));

0
 

Author Comment

by:LeanMoreTryMore
ID: 17845384
i think something wrong it calendar.set

i found some info about this ages ago... please help
0
 
LVL 92

Accepted Solution

by:
objects earned 300 total points
ID: 17845443
try this:

        DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH,-1);
        // first day of month
        c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
        Date first = c.getTime();
        System.out.println(df.format(c.getTime()));
       
        // last day of month
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date last = c.getTime();
        System.out.println(df.format(c.getTime()));
0
 

Author Comment

by:LeanMoreTryMore
ID: 17845702
i think i understand now. but change the date - 1 first before setting the calendar.

Just last question before i close off. thankyou for all

how to get convert back to the string and calendar format  (yyyy-MM-dd).

        DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH,-1);
        cal.setTime(date);
        // first day of month
        c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
        Date first = c.getTime();
        System.out.println(df.format(first));    


0
 

Author Comment

by:LeanMoreTryMore
ID: 17845725
i work it out.
thanks for all
0
 
LVL 92

Expert Comment

by:objects
ID: 17845765
> Calendar c = Calendar.getInstance(); // (The 1st)
> c.add(Calendar.DATE, -1);
> System.out.println(c.getTime());

btw, thats wrong :)

> how to get convert back to the string and calendar format  (yyyy-MM-dd).

use another SimpleDateFormat to format the date

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17845986
:-)

>>c.add(Calendar.DATE, -1);  <== i thought this one is to get the last month.

It's to get the last day of the previous month, since you told me you already had the first day of the month
0
 
LVL 92

Expert Comment

by:objects
ID: 17848100
> since you told me you already had the first day of the month

that was not stated anywhere, please try to read the question before posting :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17848125
>>
For example,

today is 01-11-2006
>>
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
table example 4 32
difference between sorce folder and folder in eclipise 3 28
Java Restore security prompts not working 10 50
Crystal Reports Licensing Questions 4 33
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question