Solved

How to i get the last date of the last month

Posted on 2006-10-31
21
6,253 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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 …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

763 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