如何在不使用 Java 中的 Calendar 并且没有时间戳的情况下获取昨天的日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22458042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:50:02  来源:igfitidea点击:

How can I get yesterday's date without using Calendar in Java and without a timestamp just the date?

javadatedate-formatsimpledateformat

提问by wheelerlc64

I have wrote a method to get the current date in the format of yyyy-MM-ddand want to be able to also create another method for getting yesterday's date, the day before the current date. All this needs is the date and not the timestamp. I am not trying to use Calendaras well. I have set up the current date this way:

我写了一个方法来获取当前日期的格式,yyyy-MM-dd并希望能够创建另一种方法来获取昨天的日期,即当前日期的前一天。所有这些需要的是日期而不是时间戳。我也不想使用Calendar。我以这种方式设置了当前日期:

public class DateWithNoTimestamp
{
       private static final String CURRENT_DATE_FORMAT = "yyyy-MM-dd";

       public final static String getCurrentDate()
       {
               DateFormat dateFormat = new SimpleDateFormat(CURRENT_DATE_FORMAT);
               Date date = new Date();
               return dateFormat.format(date);
       } 
}

This works to get the current date, now the separate method getYesertdayDate()I'm having trouble with. How can I set it up in a similar way as I did with getCurrentDate()while subtracting one day ?

这适用于获取当前日期,现在是getYesertdayDate()我遇到问题的单独方法。如何以与getCurrentDate()减去一天的方式类似的方式进行设置?

回答by Giovanni Botta

You could simply subtract 1000 * 60 * 60 * 24milliseconds from the date and format that value:

您可以简单地1000 * 60 * 60 * 24从日期中减去毫秒并格式化该值:

Date yesterday = new Date(System.currentTimeMillis() - 1000L * 60L * 60L * 24L));

That's the quick-and-dirty way, and, as noted in the comments, it might break when daylight savings transitions happen (two days per year). Recommended alternatives are the Calendar API, the Joda APIor the new JDK 8 time API:

这是一种快速而肮脏的方式,并且如评论中所述,当夏令时转换发生时(每年两天),它可能会中断。推荐的替代方案是Calendar APIJoda API或新的 JDK 8 时间 API:

LocalDate today = LocalDate.now();
LocalDate yesterday = today.minusDays(1);

回答by Roberto

You can create a Date() object for "yesterday":

您可以为“昨天”创建一个 Date() 对象:

private static final String CURRENT_DATE_FORMAT = "yyyy-MM-dd";

public final static String format(Date date) {
    DateFormat dateFormat = new SimpleDateFormat(CURRENT_DATE_FORMAT);
    return dateFormat.format(date);
}

public final static String formatToday() {
    return format(new Date());
}

public final static String formatYesterday() {
    return format(new Date(new Date().getTime() - 24*3600*1000));
}

回答by Codev

Try this:

尝试这个:

public class DateWithNoTimestamp
{
       private static final String CURRENT_DATE_FORMAT = "yyyy-MM-dd";

       public final static String getCurrentDate()
       {
           DateFormat dateFormat = new SimpleDateFormat(CURRENT_DATE_FORMAT);
           Date date = new Date();
           date .setTime(date.getTime()-24*60*60*1000);  // Subtract 24*60*60*1000 milliseconds
           return dateFormat.format(date);
       } 
}

回答by Mamun

Please Try This to get the Date of Yesterday:

请试试这个以获得昨天的日期:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    long yourDateMillis = System.currentTimeMillis()- (24 * 60 * 60 * 1000);
                 Time yourDate = new Time();
         yourDate.set(yourDateMillis);

    String formattedDate = yourDate.format("%d-%m-%Y");
    txtViewYesterday.setText(formattedDate);}

回答by dyng

I found org.apache.commons.lang3.time.DateUtilsis more intuitive and easy to use.

我发现org.apache.commons.lang3.time.DateUtils它更直观且易于使用。

For your question

对于您的问题

Date date = DateUtils.addDays(new Date(), -1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);

will do the trick. And it works well also in JDK7 or below.

会做的伎俩。它也适用于 JDK7 或更低版本。

回答by llappall

With Java 8 and later, use java.time.LocalDateclass:

对于 Java 8 及更高版本,请使用java.time.LocalDate类:

LocalDate yesterday = LocalDate.now().minusDays(1L);