0

I'm querying database and getting date in this format "01-SEP-22"

I want to convert this date into this format "yyyy-MM-dd" in Java. Is there any way I can do this.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 1
    It seems you are getting the date as a string from your database. Don’t do that. [Retrieve a `LocalDate` object.](https://stackoverflow.com/questions/43039614/insert-fetch-java-time-localdate-objects-to-from-an-sql-database-such-as-h2) Then its `toString()` method will give you the format you ask for. – Ole V.V. Mar 17 '21 at 06:37
  • 1
    Please learn that you are supposed to search before asking a question on Stack Overflow and when you post a question, tell us what your search brought up and specify how it fell short of solving your problem. It’s for your own sake since (1) you often find a better answer faster that way (2) it allows us to give preciser and more focused answers. – Ole V.V. Mar 17 '21 at 06:40
  • In this case does 22 mean 1922 or 2022? – Ole V.V. Mar 17 '21 at 19:16
  • 1
    You tagged your question simpledateformat. I recommend you don’t use `SimpleDateFormat`. That class is notoriously troublesome and long outdated. Instead use `DateTimeFormatter` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Mar 17 '21 at 20:09
  • 1
    @OleV.V. This Question is not a duplicate of [that one](https://stackoverflow.com/questions/60865121/how-to-convert-a-string-date-value-of-pattern-dd-mmm-yy-to-date-object-of-patter). This Question has the month in all caps while the other has the month name with only an initial cap. And that Question has a missing century on the year. – Basil Bourque Mar 18 '21 at 06:25

2 Answers2

1

java.time

I recommend that you use java.time, the modern Java date and time API, for your date work.

In order to parse the month abbreviation in all upper case (like SEP) we need to instruct it to apply case insensitive parsing.

We can use DateTimeFormatterBuilder to build a DateTimeFormatter with such an instruction.

private static final DateTimeFormatter oracleFormatter
        = new DateTimeFormatterBuilder()
                .parseCaseInsensitive()
                .appendPattern("dd-MMM-uu")
                .toFormatter(Locale.ROOT);

The rest goes smoothly:

    String stringFromOracle = "01-SEP-22";
    
    LocalDate date = LocalDate.parse(stringFromOracle, oracleFormatter);
    String formattedString = date.toString();
    
    System.out.println(formattedString);

Output is:

2022-09-01

For generating the string I am exploiting the fact that LocalDate.toString() gives the format that you asked for, so I am not using any formatter explicitly. The format is known as ISO 8601 and as this name says, is an international standard.

Suggestions for improvement

  1. Don’t retrieve your date as a String from Oracle. Retrieve a LocalDate directly and save the parsing.
  2. Don’t convert a date from one string format to another. In your program keep the date as a LocalDate. If you need to take string input (which is not the case here), parse the string into a LocalDate immediately. Only when you need to give string output (to the user or in data exchange with another system, for example), format the LocalDate into a string in the required format.

Links

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

You can simply use DateTimeFormatter:

public String convertDate(String dateStr) throws ParseException {
    String[] split = dateStr.split("-");
    split[1] = split[1].substring(0, 1).toUpperCase() + split[1].substring(1).toLowerCase();
    dateStr = String.join("-", split);
    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MMM-yy", Locale.ENGLISH);
    LocalDate date = LocalDate.parse(dateStr, dateFormatter);
    return date.toString();
}

@Test
public void test_convertDate() throws ParseException {
    assertEquals("2022-09-01", convertDate("01-SEP-22"));
}
  • 1
    I absolutely recommend using `DateTimeFormatter`as you suggest. It can be persuaded to parse case insensitively, see my answer, so your upper case and lower case trickery is not needed. As an aside your method cannot throw a `ParseException`, so I suggest you remove the `throws` clause. – Ole V.V. Mar 17 '21 at 20:12