groovy 上一年年初

igsr9ssn  于 2022-11-01  发布在  其他
关注(0)|答案(1)|浏览(163)

**DATE FROM:**

def format=new java.text.SimpleDateFormat("yyyyMMdd")
def cal=Calendar.getInstance()
cal.get(Calendar.YEAR);
cal.set(Calendar.MONTH, 0); 
cal.set(Calendar.DAY_OF_MONTH, 31);
[format.format(cal.getTime())]

**DATE TO:**

def format=new java.text.SimpleDateFormat("yyyyMMdd")
def cal=Calendar.getInstance()
cal.add(Calendar.DAY_OF_MONTH,-cal.get(Calendar.DAY_OF_MONTH))
[format.format(cal.getTime())]

年份变化时(2020 - 2021)-将上一年的1月与今年的1月混淆
我必须更正,以便在1月(12月报告)提取前一年31.01 - 31.12期间的数据。
该作业是错误的,因为它提取了2021年1月31日至2020年12月31日的数据

// retrieve details of the current date
def cal = Calendar.instance;
def currentYear = cal.get(Calendar.YEAR);
def currentMonth = cal.get(Calendar.MONTH);

// set the instance to the start of the previous month
if ( currentMonth == 0 ) {
cal.set(currentYear-1, 11, 1);
} else {
cal.set(currentYear, (currentMonth-1), 1);
}

// extract the date, and format to a string
Date previousMonthStart = cal.time;
String previousMonthStartFormatted = previousMonthStart.format('yyyy-MM-dd');
gmxoilav

gmxoilav1#

如果您要查找的是上一年的开始,如在您的标题,然后下面的代码:

import java.time.*

def startOfPreviousYear = LocalDate.now()
                                   .withDayOfMonth(1)
                                   .withMonth(1)
                                   .minusYears(1)

println startOfPreviousYear

def againStartingFromJanuary = LocalDate.of(2021, 1, 15)
                                        .withDayOfMonth(1)
                                        .withMonth(1)
                                        .minusYears(1)

println againStartingFromJanuary

演示了一种完成此操作的方法。运行时,将打印(now为今天的日期2021.Mar.10):

─➤ groovy solution.groovy
2020-01-01
2020-01-01

评论后更新

您可以使用以下代码获取上个月和当前月份的月末:

import java.time.* 

def endOfPreviousMonth = LocalDate.now()
                                  .withDayOfMonth(1)
                                  .minusDays(1)

def endOfCurrentMonth  = LocalDate.now()
                                  .withDayOfMonth(1)
                                  .plusMonths(1)
                                  .minusDays(1)

println "end of last month:    ${endOfPreviousMonth}"
println "end of current month: ${endOfCurrentMonth}"

打印当前日期:

end of last month:    2021-02-28
end of current month: 2021-03-31

或者如果我们是在一月:

def endOfPreviousMonth = LocalDate.of(2021, 1, 15)
                                  .withDayOfMonth(1)
                                  .minusDays(1)

def endOfCurrentMonth  = LocalDate.of(2021, 1, 15)
                                  .withDayOfMonth(1)
                                  .plusMonths(1)
                                  .minusDays(1)

println "end of last month:    ${endOfPreviousMonth}"
println "end of current month: ${endOfCurrentMonth}"

它打印:

─➤ groovy solution.groovy
end of last month:    2020-12-31
end of current month: 2021-01-31

一般来说,如果你的目标日期是基于当前日期(比如,上个月,下个月,三个月前等等),你应该尽量避免使用手动日期算法。使用java提供的api:s。日期类负责处理滚动年,滚动月,滚动日,闰年等等,所有这些你真的不想花时间自己解决的东西。

相关问题