java.util.Date: seven days ago

前端 未结 11 1755
孤城傲影
孤城傲影 2020-12-15 04:08

I have a report created in Jasper Reports which ONLY recognizes java.util.Date\'s (not Calendar or Gregorian, etc).

Is there a way to create a date 7 days prior to t

相关标签:
11条回答
  • 2020-12-15 04:34

    I'm not sure when they added these, but JasperReports has their own set of "functions" that can manipulate dates. Here is an example that I haven't tested thoroughly:

    DATE(YEAR(TODAY()), MONTH(TODAY()), DAY(TODAY()) - 7)
    

    That builds a java.util.Date with the date set to 7 days from today. If you want to use a different "anchor" date, just replace TODAY() with whatever date you want to use.

    0 讨论(0)
  • 2020-12-15 04:36

    Try this:

    Calendar c = Calendar.getInstance();
    c.add(Calendar.DAY_OF_MONTH, -7);
    return c.getTime();
    
    0 讨论(0)
  • 2020-12-15 04:39

    You can try this,

        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, -7);
        System.out.println(new java.sql.Date(c.getTimeInMillis()));
    
    0 讨论(0)
  • 2020-12-15 04:39

    Due to the heated discussion:

    The question may not have a proper answer w/o a designated timezone.

    below it is some code to work w/ the default (and hence deprecated) timezone that takes into account the default timezone daylight saving.

    Date date= new Date();
    date.setDate(date.getDate()-7);//date works as calendar w/ negatives
    

    While the solution does work, it is exactly as bogus as in terms of assuming the timezone.

    new Date(System.currentTimeMillis() - 10080*60000);//a week has 10080 minutes
    

    Please, don't vote for the answer.

    0 讨论(0)
  • 2020-12-15 04:39

    I'm doing it this way :

    Date oneWeekAgo = DateUtils.addDays(DateUtils.truncate(new Date(), java.util.Calendar.DAY_OF_MONTH), -7);
    
    0 讨论(0)
提交回复
热议问题