Android get date before 7 days (one week)
Parse the date:
Date myDate = dateFormat.parse(dateString);
And then either figure out how many milliseconds you need to subtract:
Date newDate = new Date(myDate.getTime() - 604800000L); // 7 * 24 * 60 * 60 * 1000
Or use the API provided by the java.util.Calendar
class:
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -7);
Date newDate = calendar.getTime();
Then, if you need to, convert it back to a String:
String date = dateFormat.format(newDate);
Get Date of past 7days from current in android
Use java.util.Calendar
, set it to today's date and then subtract 7 days.
Calendar cal = GregorianCalendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DAY_OF_YEAR, -7);
Date 7daysBeforeDate = cal.getTime();
Edit: In Java 8 it can be done much easier by using classes from java.time
package:
final LocalDate date = LocalDate.now();
final LocalDate dateMinus7Days = date.minusDays(7);
//Format and display date
final String formattedDate = dateMinus7Days.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(formattedDate);
Display 7 dates of current week in earlier Android
So, it is not supported for below API 26.
You can use the java.time functionality in earlier Android. Use the back-port.
So, I need an alternate solution for this.
No, you don’t.
Keep your code as-is. The back-port carries nearly the same API. So you’ll need do little more than swap your import statements.
ThreeTen-Backport
Most of the java.time functionality is back-ported to Java 6 and Java 7 in the ThreeTen-Backport project.
This project is led by the same man, Stephen Colebourne, who leads the JSR 310 spec, the java.time implementation, and Joda-Time.
ThreeTenABP
The ThreeTen-Backport project is further adapted to Android specifically in the ThreeTenABP project.
Code
ZoneId z = ZoneId.systemDefault() ; // Or ZoneId.of( "Africa/Tunis" )
LocalDate today = LocalDate.now( z ) ;
LocalDate localDate = today.with( org.threeten.bp.temporal.TemporalAdjusters.previousOrSame( DayOfWeek.SUNDAY ) ) ;
List< LocalDate > dates = new ArrayList<>( 7 ) ;
for( int i = 0 ; i < 7 ; i ++ ) {
localDate = localDate.plusDays( i ) ;
dates.add( localDate ) ;
}
How to get next seven days in android?
Try this:
SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd-MMM-yyyy");
for (int i = 0; i < 7; i++) {
Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.DATE, i);
String day = sdf.format(calendar.getTime());
Log.i(TAG, day);
}
You have one instance of calendar and are adding 1, 2, 3, 4, 5, 6, and 7 days to it without resetting it. The above solution moves object creation to be within the loop.
How to get date 7 days ago to today in Kotlin?
Use this function, pass the days ago you want:
fun getDaysAgo(daysAgo: Int): Date {
val calendar = Calendar.getInstance()
calendar.add(Calendar.DAY_OF_YEAR, -daysAgo)
return calendar.time
}
Android get previous week dates
First, you set first day of week to Sunday, but you then add 1 before getting first value, so result is Mon 5/22
to Sun 5/28
, not Sun 5/21
to Sat 5/27
.
Second, when your first loop is done, the Calendar object is sitting at 5/28
. You then go backwards, subtracting 1 before getting last value, so result is 5/27
down to 5/21
, not 5/21
to 5/27
like you said.
That leaves Calendar object sitting at 5/21
. Repeating the above, you get 5/20
down to 5/14
, not 5/14
to 5/20
.
So, to get your results as a week of Sunday to Saturday, like you told the Calendar that you wanted, add 1 after the getting the value. And to get a week in the right order, always get them incrementally. To go back to previous week, since Calendar is sitting after the week, you go back 2 weeks to start building the previous week.
public class Test {
private Calendar calendar;
public String[] getCurrentWeek() {
this.calendar = Calendar.getInstance();
this.calendar.setFirstDayOfWeek(Calendar.SUNDAY);
this.calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
return getNextWeek();
}
public String[] getNextWeek() {
DateFormat format = new SimpleDateFormat("M-dd");
String[] days = new String[7];
for (int i = 0; i < 7; i++) {
days[i] = format.format(this.calendar.getTime());
this.calendar.add(Calendar.DATE, 1);
}
return days;
}
public String[] getPreviousWeek() {
this.calendar.add(Calendar.DATE, -14);
return getNextWeek();
}
public static void main(String[] args) {
Test t = new Test();
System.out.println("Current : " + Arrays.toString(t.getCurrentWeek()));
System.out.println("Previous: " + Arrays.toString(t.getPreviousWeek()));
System.out.println("Previous: " + Arrays.toString(t.getPreviousWeek()));
System.out.println("Next : " + Arrays.toString(t.getNextWeek()));
System.out.println("Next : " + Arrays.toString(t.getNextWeek()));
}
}
Output
Current : [5-21, 5-22, 5-23, 5-24, 5-25, 5-26, 5-27]
Previous: [5-14, 5-15, 5-16, 5-17, 5-18, 5-19, 5-20]
Previous: [5-07, 5-08, 5-09, 5-10, 5-11, 5-12, 5-13]
Next : [5-14, 5-15, 5-16, 5-17, 5-18, 5-19, 5-20]
Next : [5-21, 5-22, 5-23, 5-24, 5-25, 5-26, 5-27]
Check if date is one week before from today Java
Using only Date
and Calendar
classes (and thus, compatible with any JVM from about 4-ish, I think?) you could try this sort of solution:
- Get today as a
Date
:Date now = new Date()
- Get one week ago from that, as a
Calendar
:Calendar expected = Calendar.getInstance(); expected.setTime(now); lastWeek.add(Calendar.WEEK_OF_YEAR, -1);
- Get your expired date as a
Calendar
:Calendar actual = Calendar.getInstance().setTime(expiredDate);
- Compare the year and day of year of the two calendars (you can compare other fields, but those two should be enough):
return (expected.get(Calendar.YEAR) == actual.get(Calendar.YEAR)) && (expected.get(Calendar.WEEK_OF_YEAR) == actual.get(Calendar.WEEK_OF_YEAR));
Using this, you should be able to come up with an even shorter snippet that subtracts a week from now and compares the long values of the two. Though obviously that wouldn't be comparing calendar dates, it would be comparing nanoseconds :)
How to fetch recent seven days from specific date
Parse the date:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = sdf.parse("2017-03-30");
First Solution 1) And then either figure out how many milliseconds you need to subtract:
Date newDate = new Date(myDate.getTime() - 604800000L); // 7 * 24 * 60 * 60 * 1000
Second Solution 2) Or use the API provided by the java.util.Calendar class:
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -7);
Date newDate = calendar.getTime();
Then, if you need to, convert it back to a String:
String date = dateFormat.format(newDate);
This answer is from here
EDIT:
If you need output as 2017-03-29 2017-03-28 2017-03-27 ...... 2017-03-23 then try below code
for(int i = 1; i <= 7; i++){
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -i);
Date newDate = calendar.getTime();
String date = dateFormat.format(newDate);
//here in date you can get all date from and output as 2017-03-29 2017-03-28 2017-03-27 ...... 2017-03-23
}
Hope you need this
Related Topics
Stream Live Android Audio to Server
Decompile an APK, Modify It and Then Recompile It
Android Heterogeneous Gridview Like Pinterest
:App:Lintvitalrelease' Error When Generating Signed APK
Which Android Data Storage Technique to Use
How to Change Text of a Textview in Navigation Drawer Header
How to Convert Firebase Data to Java Object...
How to Change a Bitmap's Opacity
What Is Difference Between @+Id/Android:List and @+Id/List
Send Sms Until It Is Successful
Change Listview Background - Strange Behaviour
Code Will Only Return 0.0, 0.0 Gps Coordinate While Throwing Nullpointerexception
Android Studio Convert Iso String to "America/New_York" When Adding to Event to Calendar
Background Timer Task in Jsp/Servlet Web Application