从simpledataformat获取昨天

时间:2015-11-11 03:41:20

标签: android simpledateformat

这是我今天要做多久的代码。 我可以通过使用此代码获得昨天吗?

SimpleDateFormat date_0 = new SimpleDateFormat("yyMMdd");
Date date_1 = new Date();
long date_t = Long.valueOf(date_0.format(date_1));

编辑> 我用这个解决了

SimpleDateFormat date_0 = new SimpleDateFormat("yyMMdd");
Date date_1 = new Date();
long date_t = Long.valueOf(date_0.format(date_1));
long date_2 = date_1.getTime();
Date yesterday = new Date(date_2 -= 86400000);
long date_y = Long.valueOf(date_0.format(yesterday));

5 个答案:

答案 0 :(得分:0)

我不确定但这应该有效:

def score(playerhand, comhand):
    playerscore = 0
    comscore = 0
    for i in playerhand:
        if playerhand == str('A'):
            playerscore += int(11)
        elif playerhand == str('J'):
            playerscore += int(10)
        elif playerhand == str('Q'):
            playerscore += int(10)
        elif playerhand == str('K'):
            playerscore += int(10)
        else:
            playerscore += int(playerhand) #This is where it breaks

答案 1 :(得分:0)

您可以获取当前日期,并减去86400000毫秒(相当于一天)。

           SimpleDateFormat date_0 = new SimpleDateFormat("yyMMdd");
           Date date_1 = new Date();
           long date_t = date_1.getTime();
           date_t -= 86400000;
           Date yesterday = new Date(date_t);
           System.out.println("Yesterday's Date: " + date_0.format(yesterday));

答案 2 :(得分:0)

SimpleDateFormat date_0 = new SimpleDateFormat("yyMMdd");

Calendar calendar = Calendar.getInstance(); // this is default system date
calendar.add(Calendar.DAY_OF_MONTH, -1);    // minus date to previous day 
long date_t = Long.valueOf(date_0.format(calendar.getTime())); // convert into long
System.out.println(date_0.format(calendar.getTime())); // system print 151110

答案 3 :(得分:0)

您可以使用 Joda-Time。该设计允许多个日历系统,同时仍提供简单的API。

private DateTime getPreviousDateAndTime(int previousCount){
   DateTimeFormat format = DateTimeFormat.forPattern("yyyy-MM-dd 00:00:00.000000000");
   DateTime now = new DateTime();
   DateTime expectedDate = now.minusDays(previousCount);
   return expectedDate;
}

将以下依赖项添加到 build.gradle

dependencies {
    compile 'net.danlew:android.joda:2.9.0'
}

答案 4 :(得分:0)

简单..

private Calendar calendar;

  public void getPreviousDay() {
        calendar.add(Calendar.DATE, -1);
    }