This question already has answers here:
How to subtract X days from a date using Java calendar?
(11 answers)
How to add 7 days to current date while not going over available days of a month?
(8 answers)
Closed 8 years ago.
First off thank you for taking the time to scroll through this.
I am basically a greenhorn when it comes to Java but I am working on a program that asks the user some basic info then sets a start date and then schedules service dates at bi weekly intervals between March and October from their start date.
For the start date variable, I simply set it up as:
STARTDATE = getDate();
to give the current date when the user signs up.
I have been sifting through my searches for days but I cannot figure out how to increment the service dates by 14 days to save my life.
I tried using SERVICEDATE = STARTDATE + (0, 14, 0);
but I cant make sense of whats really happening here. Any ideas?
Use the common-lang library. It has a DateUtils class which can be used for this.
Use it like this:
SERVICEDATE = DateUtils.addDays(STARTDATE, 14);
Maybe have a look at this: Java getDate date-x days
They are subtracting days but I think it can be turned into adding days too.
I suggest you use Joda Library
Date STARTDATE = getDate();
DateTime stDate = new DateTime(STARTDATE);
//Date after 14 days
DateTime rsDate = stDate.plusDays(14);
Related
This question already has answers here:
Java, Calculate the number of days between two dates [duplicate]
(10 answers)
Closed 1 year ago.
I am trying to calculate the number of days between two dates.
Even though I found many similar questions, I just cannot come up with a solution.
Date lastpickup = (Date) section_userdata.get("lastpickup");
Date today = new Date();
Instant instant_lastpickup = lastpickup.toInstant().truncatedTo(ChronoUnit.DAYS);
Instant instant_today = today.toInstant().truncatedTo(ChronoUnit.DAYS);
This is my code at the moment.
A date is read from a config and should be compared to the actual date.
With the code I have I am able to determine whether the date is the same or not, but I want to know which amount of days (ideally as an Integer) is between those two.
I want to look at the calendar days, not 24h rhythm.
Well, it was way easier than I thought.
Here's my solution
long daysCount = ChronoUnit.DAYS.between(instant_lastpickup, instant_today);
This question already has answers here:
Get current week start and end date in Java - (MONDAY TO SUNDAY)
(7 answers)
Modify the week in a Calendar
(4 answers)
Closed 4 years ago.
I have an application in which every Sunday, it checks a MySQL database for data from the past week. I am trying to find out how to get the date String for each day of the past week. My obvious first attempt was:
Calendar calendar = Calendar.getInstance();
if(calendar.DAY_OF_WEEK == 7){
java.sql.Date date = new java.sql.Date(calendar.getTime().getTime());
String dates[] = new String[7];
for(int i; i < 7; i++){
dates[i] = date.substring(0,7) + date.substring(7, date.length());
}
// Now grab data from the database where the date corresponds with one of these.
}
Today, this would work. However, if it were the 1st through the 6th of a month, this would not work as it wouldn't account for the change in month. Is there a way to get around this. I'm sure somebody has done a similar thing. Thanks.
Can you try different approach? like pass today's date to sql procedure and filter last 7 days at query level with where clause?
say where date is <= today-7
This question already has answers here:
How to get current moment in ISO 8601 format with date, hour, and minute?
(23 answers)
Calendar date to yyyy-MM-dd format in java
(11 answers)
Closed 6 years ago.
I am new to Java and trying to use Calendar object to get date and time of now as a string. I am particularly stuck at object and object conversions.
Here is the format I need (as a string):
2016-03-30T14:21:00Z
If I could just get the date and time format right, I could play around with the string but I am struggling with deprecated methods.
Thank you for replies
Your best bet is to start using Java 8's new Time API (or JodaTime if you can't use Java 8)
LocalDateTime now = LocalDateTime.now();
String isoFormat = DateTimeFormatter.ISO_INSTANT.format(now.toInstant(ZoneOffset.UTC));
System.out.println(isoFormat);
outputs 2016-03-30T17:51:38.639Z (when I tested it)
Solved my question using this link:
http://beginnersbook.com/2013/05/current-date-time-in-java/
Thanks for replies, I will also look into Java 8' time API
This question already has answers here:
Get the number of weeks between two Dates.
(19 answers)
Closed 9 years ago.
Is it possible to determine the number of weeks between 2 dates in Java/JSP? For example if date one is 2013-10-29 and date two is 2013-11-12, I would like the number of weeks to be output.
Could somebody pleas help? :-)
Joda can help you, but I'm never able to use it because of its license.
If like me, Joda is not appropriate for you, you can solve this problem as follows:
initialize endDate object
initialize startDate object
initialize weeksBetween as
milliseconds between end&start/milliseconds per day, divided by seven (integer floor, ceiling or round this).
//may need to normalize dates and set them to be both midnight or noon or some common time
output weeksBetween
You can get the milliseconds between them by converting the calendars to Date (Calendar has such a method to do this).
I lifted this from: How to calculate the total hour worked between two dates?
You can use the Joda Time library :
Object Weeks, method weeksBetween :
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Android: how to get the current day of the week (Monday, etc…) in user language?
I want to get the date of the specific day in java for android application development.
eg. For Sunday
7.8.2011
14.8.2011
21.8.2011
Date date1 = (new GregorianCalendar(2011 , Calendar.AUGUST, 28)).getTime();
Format formatter = new SimpleDateFormat("EEEE");
String s = formatter.format(date1); // s==Sunday
Take a look at the Calendar Class. You can instantiate it using the Calendar.getInstance() method.
http://download.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html
i think its something like this cal.get(Calendar.DAY_OF_WEEK)