This question already has answers here:
Java Date vs Calendar
(13 answers)
Closed 8 years ago.
I get confused by the Java API for the Date class. Everything seems to be deprecated and links to the Calendar class. So I started using the Calendar objects to do what I would have liked to do with a Date, but intuitively it kind of bothers me to use a Calendar object when all I really want to do is create and compare two dates.
Is there a simple way to do that? For now I do
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
cal.set(year, month, day, hour, minute, second);
Date date = cal.getTime(); // get back a Date object
You can use SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date d = sdf.parse("21/12/2012");
But I don't know whether it should be considered more right than to use Calendar ...
The excellent joda-time library is almost always a better choice than Java's Date or Calendar classes. Here's a few examples:
DateTime aDate = new DateTime(year, month, day, hour, minute, second);
DateTime anotherDate = new DateTime(anotherYear, anotherMonth, anotherDay, ...);
if (aDate.isAfter(anotherDate)) {...}
DateTime yearFromADate = aDate.plusYears(1);
You can try joda-time.
Related
This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
Closed 7 years ago.
I am currently trying to schedule a method for execution once per week on a day which the user will select. I know I can get the current date via:
Date date = new Date();
When setting up my TimerTask for execution, I need to increment the date by 1-6 days depending on which day of week is selected by the user. I do not see a setDay() method in the documentation and was wondering if parsing the day out, changing it, and adding back to the date object is the only way. Seems like something much more simple would be out there.
You need to use a Calendar.
The java.util.Calendar class is an abstract encapsulation of the Date object.
Calendar provides getter and setter for the date fields.
Updated to an example of incrementing the day of the week as requested:
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_WEEK,(calendar.get(Calendar.DAY_OF_WEEK)+1));
//alternative:
//calendar.add(Calendar.DAY_OF_WEEK, 1);
Date newDate = calendar.getTime();
Update: Note the java 8+ implementation using java.time
Calendar and Date have not been deprecated, you can still mix and match.
However if you want to handle time zones properly or want to do more localisation (when do you not?) then you are better off using java.time.
public static Date addDays(Date date, int days) {
GregorianCalendar calendar = getCalendar(date);
calendar.add(Calendar.DATE, days);
return calendar.getTime();
}
This should do the trick.
You probably want to use Calendar.
With a Calendar object, you can simply use Calendar.add(Calendar.DAY_OF_WEEK, 1); to add a single day
It would be easy with Calendar class.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1); // this will add number of days to current
// date
Date date = cal.getTime(); // it will return the date object
System.out.println(date);
You may be looking for Date.setDate().
Bear in mind that it's deprecated, and the docs recommend using Calendar.set(Calendar.DAY_OF_MONTH, int date).
This question already has answers here:
Why is the Date constructor deprecated, and what do I use instead?
(14 answers)
Closed 7 years ago.
The constructor java.util.Date(int,int,int) is deprecated. Is there a way to set a date easy as that in Java? What's the non-deprecated way to do this?
Date date = new Date(2015, 3, 2);
What's the non-deprecated way to do this?
Java 8 to the rescue:
LocalDate localDate = LocalDate.of(2015, 3, 2);
And then if you really really need a java.util.Date, you can use the suggestions in this question.
For more info, check out the API or the tutorials for Java 8.
By using
java.util.Calendar
is one possibility:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2015);
calendar.set(Calendar.MONTH, 4);
calendar.set(Calendar.DATE, 28);
Date date = calendar.getTime();
Keep in mind that months are 0 based, so January is 0-th month and december 11th.
Try Calendar.
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
I am sure there also is a method which takes the values you provide in your example.
Use the Calendar class, specifically the set(int year, int month, int date) for your purpose. This is from Java 7, but you'll have equivalent - setDate(), setYear() etc. - methods in older versions.
I am doing up a script where by the user will pass in string date (MM/DD/YYYY). I want to compare this string date to the "today" date to find out the days different between these 2 days.
This is my codes to get "today" date.
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = Calendar.getInstance();
returnDate = dateFormat.format(cal.getTime());
How do i go about using date to minus date? convert them to timestamp and convert it back?
Thank you
getTimeInMillis() and the relevant division will tell you how many days different. Not sure why converting it back is helpful.
If you can use JodaTime library. A similar question with usage of this library was already answered here.
In this first i get the current date and then minus by 2 then using formatter and set the same accordingly. So the ouput you will get will be 2 days before todays date, Please try below:-
Calendar cal=Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -2);
Date dt=cal.getTime();
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
String ss=format.format(dt);
System.out.println(ss);
Hi I want to iterate through a date range without using any libraries. I want to start on 18/01/2005(want to format it to yyyy/M/d) and iterate in day intervals until the current date. I have formatted the start date, but I dont know how I can add it to a calendar object and iterate. I was wondering if anyone can help. Thanks
String newstr = "2005/01/18";
SimpleDateFormat format1 = new SimpleDateFormat("yyyy/M/d");
Date date = format1.parse(newstr);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
while (someCondition(calendar)) {
doSomethingWithTheCalendar(calendar);
calendar.add(Calendar.DATE, 1);
}
Use SimpleDateFormat to parse a string into a Date object or format a Date object into a string.
Use class Calendar for date arithmetic. It has an add method to advance the calendar, for example with a day.
See the API documentation of the classes mentioned above.
Alternatively, use the Joda Time library, which makes these things easier. (The Date and Calendar classes in the standard Java API have a number of design issues and are not as powerful as Joda Time).
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
format date from 14 aug to YYYYMMDD
Hi I need to get a date in a YYYYMMDD format. I Don't know the right method for that...
I set the date with this lines of code...
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DATE, date);
Later in the code I need to extract the YYYYMMDD date value, how I can do that?
Thanks
You would use SimpleDateFormat:
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
format.setCalendar(cal);
String text = format.format(cal.getTime());
Personally I would strongly recommend that you look at using Joda Time instead though - it's a much nicer API than the built-in Date/Calendar class.
You can use the SimpleDateFormat class.
SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMDD");
String date = sdf.format(new Date());
The above code should give you the current date in the format YYYYMMDD.
I think you should look to the API documentation for SimpleDateFormat class. It provides a format method.
SimpleDateFormat
It's great for problems like this
According to the Calendar documentation, you should use the DateFormat class. The documentation for DateFormat can be found here
If these do not suit your needs, then you can make a custom wrapper class for Calendar and override the toString() method