javadatedatetimesimpledateformat

Date Pattern starting from 12 hours


I'm trying to convert two dates into this pattern: yyyy-MM-dd HH:mm:ss.SSS.

I need to retrieve two dates:

So, as of today, I need these two values:

The problem is: I get 2022-11-01 12:00:00.000.

This is the code that generates last year's date.

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

If I try adding one hour, the output will be: 2022-11-01 13:00:00.000.

Also, I can't retrieve the last day of the previous month. As I have read here, all I keep getting is the first day of the next month.

Here's the code.

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

There is something I'm missing. Any help is highly appreciated!


Solution

  • You say any help is highly appreciated, so here's an example that uses java.time, which uses today as base and calculates the two desired results as follows:

    First of current month in the last year
    Last day of last month at the end of the day
    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));
    }
    

    Output (execution date 2023-11-17):

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