我正在try 将两个日期转换为这样的模式:yyyy-MM-dd HH:mm:ss.SSS.

我需要检索两个日期:

  • go 年,同月,第1天,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分.

这是生成go 年日期的代码.

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());
}

如果我try 添加一个小时,输出将是: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());
}

我漏掉了一些东西. 任何帮助都是非常感谢的!

推荐答案

您说any help is highly appreciated,下面是一个使用java.time的示例,它使用today作为基数,并按如下方式计算两个所需的结果:

First of current month in the last year
  • 从今天减go 一年(2023-11-172022-11-17)
  • 以那个月的第一天为例(2022-11-172022-11-01)
  • 参与一天的开始(2022-11-012022-11-01 00:00:00.000)
Last day of last month at the end of the day
  • 从今天减go 一个月(2023-11-172023-10-17)
  • 转到那个月的最后一天(2023-10-172023-10-31)
  • 追加一天中的最大时间(2023-10-312023-10-31 23:59:59.999)
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

Java相关问答推荐

在Spring Boot中测试时出现SQL语法错误

@ EnableRouting注释在Kotlin项目中不工作

Java事件系统通用转换为有界通配符

如何使用Java API在Oracle ODI中运行模拟?

如何在SystemiccationRetryListenerSupport中获得类级别的spring retryable annotation中指定的标签?

Java Swing:初始化身份验证类后未检测到ATM_Interface键事件

Chunk(Int)已弃用并标记为要删除

Helidon 4和Http API

在Eclipse中调试未导出的JDK模块的Java包

如何将Java文档配置为在指定的项目根目录中生成?

有效的公式或值列表必须少于或等于255个字符

为什么Collectors.toList()不能保证易变性

如何获得凌空cookies ,并设置它在下一个请求- android

JOLT根据值删除并保留其余的json键

为什么这种递归会有这样的行为?

H2数据库仅支持%1个结果集?

java21预览未命名的符号用于try-with-resources

Maven创建带有特定类的Spring Boot jar和普通jar

更新不可变的深层嵌套字段

Java-Apache BufferedHttpEntity 在发送请求时加载整个文件