Solved

Find a future date

Posted on 2006-11-29
7
225 Views
Last Modified: 2010-03-31
Hi Experts


I would like to find what will be the date for the current  year on March first - 5 days.

I would like to get this date as an int this format :  example 20070220.


How would I do that ?

Thank you


0
Comment
Question by:blarouche
  • 4
  • 3
7 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 18037871
Calendar cal =  Calendar.getInstance();
cal.set(Calendar.MONTH, Calendar.FEBRUARY);
cal.set(Calendar.MONTH, cal.getActualMaximum(Calendar.MONTH));
cal.add(Calendar.Date, -4);
// Now format the Date
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18037887
DateFormat df = new SimpleDateFormat("yyyymmDD");
int date = Integer.parseInt(df.format(cal.getTime());
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18037891
int date = Integer.parseInt(df.format(cal.getTime()));
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:blarouche
ID: 18037960
Sorry CEHJ


I accepted the answer but I found out an error

My result give me 200611359 which is wrong
0
 

Author Comment

by:blarouche
ID: 18038111
I think there is a mistake in the code.

I changed it for :

Calendar cal = Calendar.getInstance();
            cal.set(Calendar.MONTH, Calendar.FEBRUARY);
            cal.set(Calendar.DATE,cal.getActualMaximum(Calendar.MONTH));
            cal.add(Calendar.DATE, -4);
            
            DateFormat df = new SimpleDateFormat("yyyymmDD");
            int date = Integer.parseInt(df.format(cal.getTime()));
            System.out.println(date);

But it still doesn't work
0
 

Author Comment

by:blarouche
ID: 18038660

I found something that works for me.


Calendar cal = Calendar.getInstance();
            cal.set(Calendar.MONTH, Calendar.MARCH);
            cal.set(Calendar.DATE, 01);
            cal.add(Calendar.DATE, -5);
            
            Date myDate = cal.getTime();
            DateFormat df = new SimpleDateFormat("yyyyMMdd");
            int marchdate = Integer.parseInt(df.format(myDate));
            System.out.println(marchdate);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18039611
Yes that's better actually
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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
HTTPSessionBindingListernter not configured in deployment descriptor 2 65
ejb on wildfly 5 30
how to debug htl and js pages 8 38
web project error add remove 1 27
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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:
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.

790 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