How to get date of all days in past week? [duplicate] - java

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

Related

File.lastmodified() generates wrong date and month [duplicate]

This question already has answers here:
Why is January month 0 in Java Calendar?
(18 answers)
Why Java Calendar set(int year, int month, int date) not returning correct date? [duplicate]
(3 answers)
Closed 3 years ago.
Im using File.getlastmodified() to get last modifies of files but for some reason (probably UIT) its printing wrong date and month (year is fine)
Im getting last modifed with the above method and saving it to Long
//This should ideally conver the last modified to human radable format
but its returning wrong month adn date
like----> 27/11/2018
shows as---> 28/10/2018
c = Calendar.getInstance();
Long epoch = files[i].lastModified();
epoch+=32400L;
c.setTimeInMillis(epoch); //im converting the milliseconds to human readable formate
int year=c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH);
int day=c.get(Calendar.DATE);
The value is not incorrect.
You are using the Calendar incorrectly. Checking the documentation you can see the value returned for Month.
Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.
Source
Now, for the day, I would suspect the addition of 32 seconds could be the problem. But most probably the timezone. Indeed, the method return a value on GMT.
Note that I have check with a file modified the "2019-04-17 14:52:13" and get the correct result. Also, you can format the Calendar using a SimpleDateFormat instance instead of extracting the value like this.
private static String formatEpoch(long epoch) {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(epoch));
}
Or, we can never mention this enough, using a more recent API for date with Instant.
private static String formatEpoch(long epoch) {
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
return formatter.format(Instant.ofEpochMilli(epoch).atZone(ZoneId.systemDefault()));
}
An instant is a date-time at GMT, so we add the locale timezone before formatting it with a DateTimeFormatter to provide a value like :
2019-04-17T14:52:13.118

I want 7 days ahead of current date by using java 7 not java 8 [duplicate]

This question already has answers here:
Adding days to a date in Java [duplicate]
(6 answers)
Closed 5 years ago.
String s= new java.util.Date().toString();
I am using java 7, I want to print one week ahead of this date I have used the calendar class but it always gives me IST 1970 and does not give correct answer and I want that date into string so please help me in this. I know this question is repeated but they cant solve my query?
Don't use Date, use Calendar:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 7);
System.out.println(cal.getTime());
If you want date in string format then use Dateformat . Below is the complete code ,
Date givenDate= new Date();
DateFormat dateFormatter=new SimpleDateFormat("dd-MM-yy");
Calendar c=Calendar.getInstance();
c.setTime(givenDate);
//This will print whole week after given date
for(int i=1;i<8;i++){
c.add(Calendar.DAY_OF_MONTH, 1);
System.out.println(dateFormatter.format(c.getTime()));
}

how to add number of days into a given date in the format mm/dd/yyyy? [duplicate]

This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
Closed 6 years ago.
How to add number of days into a given date in the format mm/dd/yyyy .
If my date is 9/12/2007, I want to add 30 days into the date and the result should be 10/12/2007.
I have many frequencies like Weekly, monthly, Every 2 weeks, Twice a month, Every 4 weeks, Once in 2 months, Every 3 months, Every 6 months, Every 3 months,
Annually, etc.
If we select the different frequencies from the list, the result should vary based on the frequency. Can anyone help me on this ?
Convert your date to a LocalDate, add the required values to it and then convert it back to the format you need it.
For example adding 30 days would look like this:
LocalDate d = LocalDate.of(2007,9,12).plus(30, ChronoUnit.DAYS)
And if you look at ChronoUnit you can see there are some units defined like weeks, days, months and so on...
String dt = "9/12/2007"; // Start date
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 1);

getting and setting dates with Java [duplicate]

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);

How can I get the day of the specific date in java for android application development? [duplicate]

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)

Categories