Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to i get the last date of the last month

Posted on 2006-10-31
21
Medium Priority
?
6,259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 5
  • 4
  • +1
21 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 9

Assisted Solution

by:shinobun
shinobun earned 400 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 1200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

719 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