简体   繁体   English

上一年的开始

[英]Start of previous year

**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())]

when year changes (2020 - 2021) - it confuses January of previous year with January of this year当年份变化时(2020 - 2021) - 它将上一年的一月与今年的一月混淆
I have to correct so that in January (December reporting) it extracts data for period 31.01 - 31.12.我必须进行更正,以便在 1 月(12 月报告)中提取 31.01 - 31.12 期间的数据。 of previous year.上一年的。

The job was wrong because it extracted data from 31.01.2021 to 31.12.2020作业是错误的,因为它从 31.01.2021 到 31.12.2020 提取数据

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

If all you are looking for is the start of the previous year as in your title then the following code:如果您要查找的只是标题中的上一年的开始,那么以下代码:

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

demonstrates one way to accomplish this.演示了实现此目的的一种方法。 When run, this prints (with now being today's date of 2021.Mar.10):运行时,将打印( now是今天的 2021.Mar.10 日期):

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

updated after comments评论后更新

You can get the end of previous and current months with something like this:您可以通过以下方式获得上个月和当前月份的结束:

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}"

which with current date prints:当前日期打印:

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

or if we are in january:或者如果我们在一月份:

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}"

which prints:打印:

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

In general you should try to, when possible, stay away from using manual date arithmetic when dealing with dates if your target is based on the current date (as in, previous month, next month, three months ago, etc).一般来说,如果您的目标基于当前日期(如上个月、下个月、三个月前等),则在处理日期时,您应该尽可能避免使用手动日期算法。 Use the api:s handed to you by java.使用 java 交给您的 api:s。 The date classes take care of rolling years, rolling months, rolling days, leap years, etc, all that stuff that you really do not want to spend time solving yourself.日期类处理滚动年份、滚动月份、滚动天数、闰年等,所有这些你真的不想花时间自己解决的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM