[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 425
  • Last Modified:

java getting the date now and in 6 months from now in format yyyy-mm-dd

Hi,

I need a little bit of code that will produce two strings, one of todays date and one of the date in 6 months from now in yyyy-mm-dd format

String dateNow; // i.e. 2009-04-18
String dateInSixMonthsFromNow; // i.e. 2009-10-18

I need this so that every day that I run my code, it gets dateNow, and dateInSixMonthsFromNow.

Please help - I'm still getting my head around calender in java

cheers
J

0
JamesFrog
Asked:
JamesFrog
  • 4
  • 4
  • 2
3 Solutions
 
CEHJCommented:
Tricky to define +6 months. What's your definition?
0
 
objectsCommented:
Calendar cal = Calendar.getInstance();
String dateNow = df.format(cal.getTime());
cal.add(Dalendar.MONTH, 1);
String dateInSixMonthsFromNow = df.format(cal.getTime());
0
 
objectsCommented:
df is a DateFormat

http://helpdesk.objects.com.au/java/how-do-i-format-a-date-as-a-string

> cal.add(Dalendar.MONTH, 1);

sorry, should be:

cal.add(Dalendar.MONTH, 6);

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!

 
JamesFrogAuthor Commented:
objects,

I've put the following code in my main menu, and added

import java.util.*;

but it's giving me

"DateFormat cannot be resolved to a type"

Please help
J



DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
		
		Calendar cal = Calendar.getInstance();
		String dateNow = dateFormat.format(cal.getTime());
		System.out.println (dateNow);
		
		cal.add(Calendar.MONTH, 6);
		String dateInSixMonthsFromNow = dateFormat.format(cal.getTime());
		System.out.println (dateInSixMonthsFromNow);

Open in new window

0
 
JamesFrogAuthor Commented:
Ahh I needed

import java.text.*;

but my code now gives

2009-21-18
2009-21-18

with the following code...?
DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
		
		Calendar cal = Calendar.getInstance();
		String dateNow = dateFormat.format(cal.getTime());
		System.out.println (dateNow);
		
		cal.add(Calendar.MONTH, 6);
		String dateInSixMonthsFromNow = dateFormat.format(cal.getTime());
		System.out.println (dateInSixMonthsFromNow);

Open in new window

0
 
CEHJCommented:
tRY
                Calendar cal = Calendar.getInstance();
                System.out.println (new java.sql.Date(cal.getTimeInMillis()));
                
                cal.add(Calendar.MONTH, 6);
 
                System.out.println (new java.sql.Date(cal.getTimeInMillis()));

Open in new window

0
 
CEHJCommented:
You don't need DateFormat, but if you do use it, it should be
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

Open in new window

0
 
JamesFrogAuthor Commented:
Got it working! Thanks
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		Calendar cal = Calendar.getInstance();
		String dateNow = dateFormat.format(cal.getTime());
		System.out.println (dateNow);
		
		cal.add(Calendar.MONTH, 6);
		String dateInSixMonthsFromNow = dateFormat.format(cal.getTime());
		System.out.println (dateInSixMonthsFromNow);

Open in new window

0
 
JamesFrogAuthor Commented:
Thanks all once again!
0
 
CEHJCommented:
:-)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now