0

I need to produce a simple string which should look like this:

01-03-2019 - 31-03-2019

The month and year should be of current date.

I have an expression like this:

new GregorianCalendar().getActualMinimum(Calendar.DAY_OF_MONTH) + " - " + 
new GregorianCalendar().getActualMaximum(Calendar.DAY_OF_MONTH)

But this produces a string like this:

1 - 31

How can I change the statement to get a full date in the format mm-DD-yyyy? It has to work without variables, as I am willing to use it as an variable expression in JasperSoft Studio...

hc0re
  • 1,806
  • 2
  • 26
  • 61
  • I recommend you don’t use `GregorianCalendar`. That class is poorly designed and long outdated. Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Mar 26 '19 at 10:22
  • Are you tied to some older Java version? A method reference might also help, is it out of bounds? A potential issue with the code you have is if it runs over midnight, the two calls to `new GregorianCalendar()` may produce different dates. – Ole V.V. Mar 26 '19 at 10:35

4 Answers4

1

If you want to do it in a variableless way and in a single statement using Gregorian Calendar only then you can use the below code:

System.out.println(
        String.format("%2s", Calendar.getInstance().getActualMinimum(Calendar.DAY_OF_MONTH)).replace(" ", "0") +
        "-" +
        String.format("%2s", (Calendar.getInstance().get(Calendar.MONTH) + 1)).replace(" ", "0") +
        "-" +
        Calendar.getInstance().get(Calendar.YEAR) +
        " - " +
        String.format("%2s", Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH)).replace(" ", "0") +
        "-" +
        String.format("%2s", (Calendar.getInstance().get(Calendar.MONTH) + 1)).replace(" ", "0") +
        "-" +
        Calendar.getInstance().get(Calendar.YEAR));
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • That totally solved my problem, thanks! – hc0re Mar 26 '19 at 11:01
  • In that case why not just `String.format("%02d-%02d-%d - %02d-%02d-%d", Calendar.getInstance().getActualMinimum(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.MONTH) + 1, Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.MONTH) + 1, Calendar.getInstance().get(Calendar.YEAR))`? (still risking confusing results when run at midnight) – Ole V.V. Mar 26 '19 at 11:08
0

You can use Java 8's time APIs for this, e.g.:

YearMonth yearMonth = YearMonth.now();
System.out.println(yearMonth.atDay(1));
System.out.println(yearMonth.atEndOfMonth());

If you want to display both in single line then you can use the following:

System.out.println(yearMonth.atDay(1) + " - " + yearMonth.atEndOfMonth());

Update

You can use DateTimeFormatter to format it using specified pattern, e.g.:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
YearMonth yearMonth = YearMonth.now();
System.out.println(yearMonth.atDay(1).format(formatter) + " - " + yearMonth.atEndOfMonth().format(formatter));
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
0

It's simple, use LocalDate :

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate startMonth = LocalDate.now().withDayOfMonth(1);
LocalDate finishMonth = startMonth.plusMonths(1).minusDays(1);
System.out.println(startMonth.format(formatter) + " - " +  finishMonth.format(formatter));
Ilya
  • 720
  • 6
  • 14
0

Java 8 or later: java.time

    ((Supplier<String>) () -> {
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-uuuu");
        YearMonth ym = YearMonth.now(ZoneId.of("Asia/Pyongyang"));
        return ym.atDay(1).format(dateFormatter) + " - " + ym.atEndOfMonth().format(dateFormatter);
    })
            .get();

Output when running just now:

01-03-2019 - 31-03-2019

I wanted to call YearMonth.now only once so I don’t risk getting two different results if running at midnight between two months. While it’s unlikely, it would also be very confusing.

Java 6 or 7: ThreeTen Backport

    new Object() {
        String build() {
            DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-uuuu");
            YearMonth ym = YearMonth.now(ZoneId.of("Asia/Pyongyang"));
            return ym.atDay(1).format(dateFormatter) + " - " + ym.atEndOfMonth().format(dateFormatter);
        }
    }.build();

Java 7 or earlier without external library (discouraged)

    new Object() {
        String build() {
            SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
            Calendar cal = new GregorianCalendar();
            cal.set(Calendar.DAY_OF_MONTH, 1);
            String startString = formatter.format(cal.getTime());
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
            return startString + " - " + formatter.format(cal.getTime());
        }
    }.build();

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161