This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
How to compare dates in Java? [duplicate]
(11 answers)
Closed 2 years ago.
How to compare two dates in Java by incrementing Date?
String dt = "2008-01-01"; // Start date
String et="2008-01-10";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
Date startDate=sdf.parse(dt);
Date endDate=sdf.parse(et);
Date incDate;
// dt is now the new date
do
{
System.out.println("hey i am called.....");
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 1); // number of days to add
dt = sdf.format(c.getTime());
System.out.println("Incremet Date"+dt);
incDate=sdf.parse(dt);
}
while(endDate.compareTo(incDate)>=0);
first you should start using the newer classes like #TomStroemer pointed out.
I think you want to get the number of days between the two dates ?
LocalDate startDate = LocalDate.parse("2008-01-01");
LocalDate endDate = LocalDate.parse("2008-01-10");
Period period = Period.between(startDate, endDate);
System.out.println(period.getDays());
Should print 8 because there are 8 days between. Haven't tested that code.
See the following docs:
Period: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html
LocalDate: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
And really provide more details.
Related
This question already has answers here:
Get Date based on day count in Java
(7 answers)
Closed 6 years ago.
I got days in year in format 1-366
How do I convert them to actuall date like 12.12. etc.? + how it's with the 29.2.?
Thanks for answer!
Seems easy to do with Calendar:
final Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, 366);
final Date date = new Date(calendar.getTimeInMillis());
And if you want to offset a year for example:
calendar.add(Calendar.YEAR, 1);
You can use joda-time-android:
MutableDateTime mutableDateTime = new MutableDateTime();
mutableDateTime.setYear(2012);
mutableDateTime.setDayOfYear(366);
This question already has answers here:
Adding a number to the day or month or year in a date [duplicate]
(4 answers)
Closed 8 years ago.
I have a assignment for a class where we take in a date then see if the date given is valid. If it is we then add a day onto it. Where I am having trouble is after I check that the date is valid it is saved to
Date appointment = new Date();
appointment = new Date(month, day, year);
Date.advanceDate(appointment);
in a different file called Date.java
public static void advanceDate(Date aDate){
//here is where I need to read the date in aDate
}
After searching online through the Java api I haven't been able to find a way to add a day onto appointment or get the day, month, and year from appointment add a day to that and then save it as a new Date
what I have tried is doing after looking at http://docs.oracle.com/javase/8/docs/api/java/util/Date.html
aDate.getDay();
but eclipse tells me "the method getDate()is undefined for the type Data
aDate.toString();
this doesn't return the month date and year it returns its location in memery
every solution I've found online uses Calender which seems to have replaced Date
I believe you are looking for a Calendar and a DateFormat,
int year = 2015;
int month = Calendar.FEBRUARY; // <-- this is actually a 1.
int date = 8;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.set(year, month, date);
cal.add(Calendar.DAY_OF_MONTH, 1);
System.out.println(df.format(cal.getTime()));
Output is
2015-02-09
1FEBRUARY.
This question already has answers here:
Difference in days between two dates in Java?
(19 answers)
Closed 8 years ago.
i am trying to get the difference between two dates. one of the dates was parsed from a string(dateEmployd) and the other date is the current date (currentDate). This is what i did to get the dates...
public static Date getActiveService(String DtEmplydString){
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");//formater for parsed String date
Date dateEmployd, currentDate,periodDifference = null;
try{
dateEmployd = ft.parse(DtEmplydString);
currentDate = new Date();
}catch(ParseException e){
JOptionPane.showMessageDialog(null, e);
}
return periodDifference;
}
Now, i am meant to return periodDifference but i dont know how i would find the difference betweent the two dates (dateEmployd and currentDate) and display it in years or days or a combination of both.
please guys much help is needed. thanks in advance...
Take a look at the jodatime library. They have functions like
DateTime dateEmployd = DateTimeFormat.forPattern("yyyy-MM-dd").parse(DtEmplydString);
Years.yearsBetween(dateEmployd, DateTime.now())
The same for Days.daysbetween, Seconds, Hours etc.
long diff = date2.getTime() - date1.getTime();
You could try out the Java.Calendar for date functions because Date is deprecated .
Here is an example with Cdate comparators
Calendar start = Calendar.getInstance();
start.setTime(from ( your initial Date object here ) );
Calendar end = Calendar.getInstance();
end.setTime(new Date());
int actualDays = start.compareTo(end);
This question already has answers here:
How to get the date 7 days earlier date from current date in Java [duplicate]
(6 answers)
Closed 8 years ago.
I have today's date in this string format 2014-05-08 and I needed to get the date of 2 weeks prior the currents date.
So the data I should be getting back is - 2014-04-24.
String currentDate= dateFormat.format(date); //2014-05-08
String dateBefore2Weeks = currentDate- 2 week;
But I am not sure how do I extract date of two weeks prior to current date in Java?
Use Calendar to modify your Date object:
//method created for demonstration purposes
public Date getDateBeforeTwoWeeks(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, -14); //2 weeks
return calendar.getTime();
}
Use the method above in your code:
String currentDate= dateFormat.format(date); //2014-05-08
String dateBefore2Weeks = dateFormat.format(getDateBeforeTwoWeeks(date));
Java now has a pretty good built-in date library, java.time bundled with Java 8.
import java.time.LocalDate;
public class Foo {
public static void main(String[] args) {
System.out.println(LocalDate.parse("2014-05-08").minusWeeks(2));
// prints "2014-04-24"
}
}
Parse the date using a SimpleDateFormat into a Date object
Use a Calendar object to subtract 14 days from that date
Format the resulting date using the same SimpleDateFormat
worth having a look into joda-time api [http://joda-time.sourceforge.net/userguide.html].
This question already has answers here:
How to add days to a date in Java
(3 answers)
Closed 7 years ago.
I have an app in which I need to get the year, month and the day seperate from the next day as an String.
Can I do tis somehow with
SimpleDateFormat day = new SimpleDateFormat("dd");
String Day = day.format(new Date()+1);
or how can I get those? Please help me I'm a total beginner.
SimpleDateFormat sdFormat = new SimpleDateFormat("dd");
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
String day = sdFormat.format( calendar.getTime() )
Newer answer for Java 8 using time API.
LocalDate tomorrow = LocalDate.now().plusDays(1);
int year = tomorrow.getYear();
String month = tomorrow.getMonth().toString();
int dayOfMonth = tomorrow.getDayOfMonth();