获取过去7天的日期从当前在android

unftdfkk  于 2022-12-31  发布在  Android
关注(0)|答案(5)|浏览(492)

我试图获取今天日期之前7天的日期。我使用SimpleDateFormat获取今天的日期。

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

请引导我通过这个
更新了我认为最有用的答案

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

谢谢你的回复

zazmityj

zazmityj1#

使用java.util.Calendar,将其设置为今天的日期,然后减去7天。

Calendar cal = GregorianCalendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DAY_OF_YEAR, -7);
Date 7daysBeforeDate = cal.getTime();

**编辑:**在Java 8中,使用java.time包中的类可以更容易地完成:

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

u1ehiz5o2#

你可以试试这个,

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
*/
yfjy0ee7

yfjy0ee73#

Android get date before 7 days (one week)

Date myDate = dateFormat.parse(dateString);

然后算出你需要减去多少毫秒:

Date newDate = new Date(myDate.getTime() - 604800000L); // 7 * 24 * 60 * 60 * 1000

或者使用java.util.Calendar类提供的API:

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

gab6jxml4#

你可以使用这个Kotlin函数来获取当前日期之前的任何日期。

/**
 * get specific date before current date
 * [day] number of day
 * [month] number of month
 * [year] number of year
 * [count] number of day, month, year
 *
 * return date
 */
fun getBeforeDate(day: Boolean = false, month: Boolean = false, year: Boolean = false, count: Int = 0): String{

    val currentCalendar = Calendar.getInstance()
    val myFormat = "dd/MM/yyyy"  // you can use your own date format
    val sdf = SimpleDateFormat(myFormat, Locale.getDefault())
    if (day){
        currentCalendar.add(Calendar.DAY_OF_YEAR, -count)
    }else if(month){
        currentCalendar.add(Calendar.MONTH, -count)
    }else if(year){
        currentCalendar.add(Calendar.YEAR, -count)
    }else{
        // if user not provide any value then give current date
        currentCalendar.add(Calendar.DAY_OF_YEAR, 0)
        // or you can throw Exception
        //throw Exception("Please provide at least one value")
    }

    return sdf.format(currentCalendar.time)
}
knsnq2tg

knsnq2tg5#

fun getBeforeDate(day: Boolean = false, month: Boolean = false, year: Boolean = false, count: Int = 0): String{

    val currentCalendar = Calendar.getInstance()
    val myFormat = "dd/MM/yyyy"  // you can use your own date format
    val sdf = SimpleDateFormat(myFormat, Locale.getDefault())
    if (day){
        currentCalendar.add(Calendar.DAY_OF_YEAR, -count)
    }else if(month){
        currentCalendar.add(Calendar.MONTH, -count)
    }else if(year){
        currentCalendar.add(Calendar.YEAR, -count)
    }else{
        // if user not provide any value then give current date
        currentCalendar.add(Calendar.DAY_OF_YEAR, 0)
        // or you can throw Exception
        //throw Exception("Please provide at least one value")
    }

    return sdf.format(currentCalendar.time)
}

相关问题