Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to i get the last date of the last month

Posted on 2006-10-31
21
Medium Priority
?
6,260 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

886 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