Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Date of past 7days from current in android

Tags:

android

I am trying to fetch the date 7days prior to today's date. I am using SimpleDateFormat to fetch today's date.

SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy");

Please guide me through this

Updated answer which I found most useful

SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
String currentDateandTime = sdf.format(new Date());
Date cdate=sdf.parse(currentDateandTime);
Calendar now2= Calendar.getInstance();
now2.add(Calendar.DATE, -7);
String beforedate=now2.get(Calendar.DATE)+"/"+(now2.get(Calendar.MONTH) + 1)+"/"+now2.get(Calendar.YEAR);
Date BeforeDate1=sdf.parse(beforedate);
cdate.compareTo(BeforeDate1);

Thank you for you reply

like image 438
Sumit Avatar asked Jun 30 '12 07:06

Sumit


3 Answers

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);
like image 75
Leszek Avatar answered Oct 04 '22 17:10

Leszek


You can try out this,

import java.util.Calendar;

public class AddDaysToCurrentDate {

  public static void main(String[] args) {

    //create Calendar instance
    Calendar now = Calendar.getInstance();

    System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1)
                        + "-"
                        + now.get(Calendar.DATE)
                        + "-"
                        + now.get(Calendar.YEAR));

    //add days to current date using Calendar.add method
    now.add(Calendar.DATE,1);

    System.out.println("date after one day : " + (now.get(Calendar.MONTH) + 1)
                        + "-"
                        + now.get(Calendar.DATE)
                        + "-"
                        + now.get(Calendar.YEAR));


    //substract days from current date using Calendar.add method
    now = Calendar.getInstance();
    now.add(Calendar.DATE, -10);

    System.out.println("date before 10 days : " + (now.get(Calendar.MONTH) + 1)
                        + "-"
                        + now.get(Calendar.DATE)
                        + "-"
                        + now.get(Calendar.YEAR));

  }
}

/*
Typical output would be
Current date : 12-25-2007
date after one day : 12-26-2007
date before 10 days : 12-15-2007
*/
like image 29
RPB Avatar answered Oct 04 '22 17:10

RPB


Android get date before 7 days (one week)

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:

and finally

String date = dateFormat.format(newDate);
like image 41
Dheeresh Singh Avatar answered Oct 04 '22 18:10

Dheeresh Singh