日期模式从 12 小时开始

问题描述 投票:0回答:1

我正在尝试将两个日期转换为这种模式:

yyyy-MM-dd HH:mm:ss.SSS

我需要检索两个日期:

  • 去年同月第一天 00:00:00.000
  • 上个月最后一天,23:59:59.999

所以,从今天开始,我需要这两个值:

  • 2022-11-01 00:00:00.000
  • 2023-10-31 23:59:59.999

问题是:我明白了

2022-11-01 12:00:00.000

这是生成去年日期的代码。

private String getLastYear() {
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    Date date = new Date();
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.add(Calendar.YEAR, -1);
    c.set(Calendar.DAY_OF_MONTH, 1);
    c.set(Calendar.HOUR, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    return format.format(c.getTime());
}

如果我尝试添加一小时,输出将是:

2022-11-01 13:00:00.000

此外,我无法检索上个月的最后一天。正如我在here读到的那样,我得到的只是下个月的第一天。

这是代码。

private String getPreviousMonth() {
    DateFormat format = new SimpleDateFormat(DATE_PATTERN);
    Calendar c = Calendar.getInstance();
    Date date = new Date();
    c.setTime(date);
    c.add(Calendar.MONTH, -1);
    c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
    c.set(Calendar.HOUR, 23);
    c.set(Calendar.MINUTE, 59);
    c.set(Calendar.SECOND, 59);
    c.set(Calendar.MILLISECOND, 999);
    return format.format(c.getTime());
}

我缺少一些东西。 非常感谢任何帮助!

java date datetime simpledateformat
1个回答
0
投票

您说非常感谢任何帮助,所以这里有一个使用

java.time
的示例,它使用today作为基础,并计算两个所需的结果,如下所示:

去年当月第一天
  • 从今天减去一年 (
    2023-11-17
    2022-11-17
    )
  • 取该月的第一天 (
    2022-11-17
    2022-11-01
    )
  • 涉及一天的开始 (
    2022-11-01
    2022-11-01 00:00:00.000
    )
public static void main(String[] args) {
    LocalDateTime lastYearSameMonthFirstDay
                        // today (without time of day)
                        = LocalDate.now()
                                   // subtract 1 from the year
                                   .minusYears(1)
                                   // go to the first day of month 
                                   .with(TemporalAdjusters.firstDayOfMonth())
                                   // append the start of the day
                                   .atStartOfDay();
    
    LocalDateTime lastDayOfPreviousMonth
                        // today (date only as above)
                        = LocalDate.now()
                                   // subract 1 from the month value
                                   .minusMonths(1)
                                   // go to the last day of that month
                                   .with(TemporalAdjusters.lastDayOfMonth())
                                   // append the end of the day
                                   .atTime(LocalTime.MAX);
    // prepare a formatter for the output
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS");
    // and print both LocalDateTimes formatted as desired
    System.out.println(lastYearSameMonthFirstDay.format(dtf));
    System.out.println(lastDayOfPreviousMonth.format(dtf));
}

输出(执行日期2023-11-17):

2022-11-01 00:00:00.000
2023-10-31 23:59:59.999
© www.soinside.com 2019 - 2024. All rights reserved.