Solved

How to i get the last date of the last month

Posted on 2006-10-31
21
6,243 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
Comment Utility
Calendar c = Calendar.getInstance(); // (The 1st)
c.add(Calendar.DATE, -1);
System.out.println(c.getTime());
0
 

Author Comment

by:LeanMoreTryMore
Comment Utility
very thanks for the quick response.

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

Expert Comment

by:CEHJ
Comment Utility
Just use a DateFormat

DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
System.out.println(df.format(c.getTime()));
0
 
LVL 9

Assisted Solution

by:shinobun
shinobun earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:LeanMoreTryMore
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
i work it out.
thanks for all
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
Comment Utility
:-)

>>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
Comment Utility
> 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
Comment Utility
>>
For example,

today is 01-11-2006
>>
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now