-1

I am receiving a date in this format from API: 20200917. How can I convert this into a date?

0009laH
  • 1,960
  • 13
  • 27
SRP
  • 3
  • 4
  • What have you tried so far? Can you show us your attempt (in code)? You could also specify the type of the *date* you are receiving. One could guess it's a `String` but one cannot be sure... – deHaar Sep 24 '20 at 08:31
  • its in string 20200917 i.e 2020-09-2017 – SRP Sep 24 '20 at 08:45

2 Answers2

1

I assume you are receiving a String representation of a date (not a java.util.Date) which you want to convert to a different format.

One way would be a String manipulation, which shouldn't be the first choice.

Another way would be to use the outdated classes java.util.Date and java.text.SimpleDateFormat to reformat that date String (this is already shown in another answer). But this would neither be my choice due to the use of an old and troublesome API.

Here's how you can do it with java.time (since Java 8):

Java:

public static void main(String[] args) {
    // example input
    String input = "20200917";
    // parse the input String with a formatter that can handle the given format
    LocalDate localDate = LocalDate.parse(input, DateTimeFormatter.BASIC_ISO_DATE;
    /*
     * now that you have a LocalDate, you can use a custom or built-in formatter to
     * create a differently formatted String (built-in one used here)
     */
    String output = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
    // and then you can output the result
    System.out.println(String.format("%s ==> %s", input, output));
}

Kotlin:

fun main() {
    val input = "20200917"
    val localDate = LocalDate.parse(input, DateTimeFormatter.BASIC_ISO_DATE
    val output = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE)
    println("$input ==> $output")
}

The output of each of these snippets

20200917 ==> 2020-09-17
deHaar
  • 17,687
  • 10
  • 38
  • 51
0

EDIT

Hi you can do like this:

String trDate="20200917";    
Date tradeDate = new SimpleDateFormat("yyyyMMdd", 
Locale.ENGLISH).parse(trDate);
String krwtrDate = new SimpleDateFormat("yyyy-MM-dd", 
Locale.ENGLISH).format(tradeDate);
shahram_javadi
  • 257
  • 1
  • 12
  • 1
    I don't know what it is but it is not an unix timestamp. Gives 08/22/1970 @ 7:21pm (UTC) and 02/21/2610 @ 9:50pm (UTC) when converted to ms. – Gregory Nowik Sep 24 '20 at 08:35
  • 1
    @GregoryNowik consider my edited answer – shahram_javadi Sep 24 '20 at 08:49
  • 1
    For an output that consists of numbers and hyphons only, a `Locale` isn't necessary, I think. And I really think one should do this using `java.time`... – deHaar Sep 24 '20 at 08:56
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Sep 24 '20 at 16:51