Basically, I've got a little program that uses date.
Date current = new Date();
current.setDate(current.getDay() + time1);
When I do this it adds to the day, but say time1 = 30 then the month doesn't change when I print the date out. I hope this makes sense I'm kinda new to this.
Use a Calendar to perform date arithmetic and a DateFormat to display the result. Something like,
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, 30);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(df.format(cal.getTime()));
Use this method
public static Date addDaystoGivenDate(Integer days, Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, days);
return cal.getTime();
}
Related
I'm trying to do something with a SimpleDateFormat, but after I read the Javadoc, I only got more confused. I want two methods, one for the timezone and one for the current time and date. My format should look like this:
Time Zone: GMT +01:00
Time and Date: Wednesday 17/04/2013, 20:38:34
I took code from the internet once for time. That worked fine:
private String getFormattedTime() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
return sdf.format(cal.getTime());
}
This will output: 20:41:34
Now for my other format, I tried something like this (I wasn't completely done yet):
private static String getFormattedDate() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEEEEEEEE DD/MM/yyyy, HH:mm:ss");
return sdf.format(cal);
}
private static String getTimeZone() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("z");
return sdf.format(cal.getTimeZone());
}
If you'd run this code, you get an IllegalArgumentException at the first return line.
It seems like the Javadocs don't give any example on how to use this.
SimpleDateFormat acts on Dates, not Calendars. To convert, use .getTime(), so your code should read:
private static String getFormattedDate() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEEEEEEEE dd:MM:yyyy, HH:mm:ss");
return sdf.format(cal.getTime());
}
private static String getTimeZone() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("z");
return sdf.format(cal.getTime());
}
However, using joda is probably a better way to go for reasons I could write a PhD thesis on.
Ok so I am trying to create a date in this format:
SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yy");
I am having trouble calculating that date so that it gives me 1/1/13.
Date newdate = new Date (136199001);
String date = dateformat.format(newdate);
However I can't work out how to do it to get to my desired date. I know I am suppose to work it out from 01/01/70 but I am having trouble. The question : what is the formula to work the date out?
I would say that what you are looking for is this:
new SimpleDateFormat("dd/MM/yy").parse("1/1/13");
You can use calendar object for a specific date. It is much easier.
Calendar cal = Calendar.getInstance();
cal.set(2013, 0, 1); //1st january 2013
Date date = cal.getTime();
SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yy");
String dateStr = dateformat.format(date);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get yesterday's date using Date
What is an elegant way set to a Java Date object's value to yesterday?
With JodaTime
LocalDate today = LocalDate.now();
LocalDate yesterday = today.minus(Period.days(1));
System.out.printf("Today is : %s, Yesterday : %s", today.toString("yyyy-MM-dd"), yesterday.toString("yyyy-MM-dd"));
Do you mean to go back 24 hours in time.
Date date = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000L);
or to go back one day at the time same time (this can be 23 or 25 hours depending on daylight savings)
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
These are not exactly the same due to daylight saving.
Convert the Date to a Calendar object and "roll" it back a single day. Something like this helper method take from here:
public static void addDays(Date d, int days)
{
Calendar c = Calendar.getInstance();
c.setTime(d);
c.add(Calendar.DATE, days);
d.setTime(c.getTime().getTime());
}
For your specific case, just pass in days as -1 and you should be done. Just make sure you take into consideration the timezone/locale if doing extensive date specific manipulations.
you can try the follwing code:
Calendar cal = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Today's date is "+dateFormat.format(cal.getTime()));
cal.add(Calendar.DATE, -1);
System.out.println("Yesterday's date was "+dateFormat.format(cal.getTime()));
As many people have already said use Calendar rather than date.
If you find you really want to use dates:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, -24);
cal.getTime();//returns a Date object
Calendar cal1 = Calendar.getInstance();
cal1.add(Calendar.DAY_OF_MONTH, -1);
cal1.getTime();//returns a Date object
I hope this helps.
tomred
You can try the following example to set it to previous date.
Calendar cal = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
System.out.println("Today's date is " +dateFormat.format(cal.getTime()));
cal.add(Calendar.DATE, -1);
System.out.println("Yesterday's date was "+dateFormat.format(cal.getTime()));
I need to add 28 days to a Date - I have tried this:
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = df.parse("01/10/2012");
long week = 1000 * 60 * 24 * 7;
date1.setTime(date1.getTime() + week);
but I got an error on this line: Date date1 = df.parse("01/10/2012");
the error: Type mismatch: cannot convert from java.util.Date to java.sql.Date
I also tried this:
Date Mydate = new Date(02,04,2012);
Calendar cal = Calendar.getInstance();
cal.setTime(Mydate);
cal.add(Calendar.DATE, 10); // add 10 days
Mydate = (Date) cal.getTime();
but I got an error when trying to see the Mydate value.
You need to change this line:
import java.sql.Date;
to this:
import java.util.Date;
Once you've done that, I think the best approach is:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2012);
cal.set(Calendar.MONTH, 3); // NOTE: 0 is January, 1 is February, etc.
cal.set(Calendar.DAY_OF_MONTH, 2);
cal.add(Calendar.DAY_OF_MONTH, 10); // add 10 days
Date date = cal.getTime();
This works for me:
public static void main(String args[]) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
Date date1 = df.parse("01/10/2012");
System.out.println(date1);
Calendar cal = Calendar.getInstance();
cal.setTime(date1);
cal.add(Calendar.DAY_OF_MONTH, 28); // add 28 days
date1 = (Date) cal.getTime();
System.out.println(date1);
}
Try code below:
Calendar MyDate= Calendar.getInstance();
long sum = MyDate.getTimeInMillis() + 2419200000; //28 days in milliseconds
MyDate.setTimeInMillis(sum);
I got a list of dates as String
for example date1->'11-11-2010' and date2->'12-01-2011'
I want to print all the dates between these two dates..
I tried to work with cal.add() but am not able to set my date1 to my cal.. if i do so i get null p
below code should do the trick for you.
String date1 = "11-11-2010";
String date2 = "12-01-2011";
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(format.parse(date1));
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(format.parse(date2));
Date currentDate = calendar1.getTime();
while(!currentDate.equals(cal2.getTime())){
calendar1.add(Calendar.DAY_OF_MONTH, 1);
currentDate = cal1.getTime();
System.out.println(currentDate);
}