-1

Java noob here. Is there a way to show the date in Calendar class aside from .getTime() method? I want something as close as possible to dd/mm/yyyy. I could make a method that would split the string returned by getTime method and choose the certain items there to form the date format I want, brute forcing my way into it. I am wondering if there's an easier way or a built-in method for this.

I am solving a problem that involves dates. I just noticed that doing a while loop, with "per day" increment using .add(Calendar.DAY_OF_MONTH, 1) could be a way to check each day for a given condition. The next problem was to return the date that hit the condition. That's what got me to java.util.Calendar anyway.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
mike
  • 41
  • 4
  • https://stackoverflow.com/questions/7882025/convert-string-to-date-then-format-the-date – OH GOD SPIDERS Sep 20 '17 at 09:58
  • 6
    `getTime()` returns a `java.util.Date` - it sounds like you're trying to *format* the `Calendar`... you could use `SimpleDateFormat`, but you'd be better off moving to the `java.time` API if at all possible. – Jon Skeet Sep 20 '17 at 09:58
  • @JonSkeet I am solving a problem that involves dates. I just noticed that doing a while loop, with "per day" increment using .add(Calendar.DAY_OF_MONTH, 1) could be a way to check each day for a given condition. The next problem was to return the date that hit the condition. That's what got me to java.util.Calendar anyway. I will check java.time API. Thanks. – mike Sep 20 '17 at 10:08
  • 3
    Please do - basically the java.util.Date and java.util.Calendar APIs are *horrible* compared with java.time.*. But I'd try to work in the domain as far as possible, too - you say you want to "return the date that hit the condition" - I would return that as a `LocalDate` rather than as a `String`. – Jon Skeet Sep 20 '17 at 10:13
  • You want the `LocalDate` class from the `java.time` package that @JonSkeet already mentioned. And you want something like `myLocalDate = myLocalDate.plusDays(1)`. For formatting, use a `DateTimeFormatter`. – Ole V.V. Sep 20 '17 at 13:10
  • 1
    @mike, when you want to add more information to your question (which is often appreciated), it’s better to edit the question than to post a comment. This time I did it for you. – Ole V.V. Sep 20 '17 at 13:13
  • 1
    You are using the wrong classes. They are terribly designed, confusing, troublesome, and now supplanted by the java.time classes. Your issues have been asked and answered many times already on Stack Overflow. Always search thoroughly before posting. Search for: LocalDate, ZonedDateTime, and DateTimeFormatter. Also, when posting, focus like a laser on one particular programming issue; your Question here goes off into multiple directions. – Basil Bourque Sep 20 '17 at 21:23

3 Answers3

8

This will help - Javadoc

Create a static method and Parse the date in any format u want using SimpleDateFormat

roronoa_zoro
  • 619
  • 4
  • 17
2

java.time

I recommend you use the modern Java date and time API known as java.time or JSR-310. For example:

    final LocalDate beginDate = LocalDate.of(2017, Month.JANUARY, 1);
    final LocalDate endDate = LocalDate.of(2020, Month.DECEMBER, 31);
    final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu");

    LocalDate currentDate = beginDate;
    while (currentDate.isBefore(endDate) && ! fulfilsCondition(currentDate)) {
        currentDate = currentDate.plusDays(1);
    }
    if (fulfilsCondition(currentDate)) {
        System.out.println("This date hit the condition: " + currentDate.format(dateFormatter));
    } else {
        System.out.println("No date in the range hit the condition");
    }

I trust you to fill in the test on your condition in the two places in the code. Depending on how you do that, the code will print for example:

This date hit the condition: 25/09/2018

If you are not yet using Java 8 or later, you will need to use the ThreeTen Backport in order to use the modern API.

Avoid the outdated Calendar class

The classes Calendar, SimpleDateFormat and friends are long outdated, and the modern API that I am using is much nicer and more natural and straightforward to work with. The old classes have been around since about Java 1, so there are very many websites telling you that you should use them. This is no longer true.

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

The easiest method to format your date can be done with the SimpleDateFormat class:

Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
System.out.println( sdf.format(calendar.getTime()) );

You can find the patterns to modify the Format here: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Black Phantom
  • 257
  • 2
  • 10