Java String to Date Conversion in different format

date, formatting, java, string

Solution

--- Answer updated due to commentary ---

Ok, so the API you are using demands a String, which represents a Date in the format of `2012-04-20`

You then need to parse the incorrectly formatted `Date` and then format it again in the needed format.

String trDate="20120106";    
Date tradeDate = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH).parse(trDate);
String krwtrDate = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).format(tradeDate);

Also note that I changed your "month" designator, as you have made a very common mistake of using `m` for month. Due to the "hours:minutes:seconds" date formats have two common "names" that both start with `m`, minutes and months. Uppercase `M` is therefore used for months, while lowercase `m` is used for minutes. I'll bet that this is the real reason you're encountering problems.

--- Original post follows ---

If your APIneeds a `java.util.Date`, then you don't need to do as much as you have. You actually have the `java.util.Date` with just the two lines

String trDate="20120106";    
Date tradeDate = new SimpleDateFormat("yyyymmdd", Locale.ENGLISH).parse(trDate);

But this solution might not make sense without a quick review of what `java.util.Date`s are. `Dates` are things, but how they are presented is divorced from what they are.

At any moment in time, there is one `Date` instance that describes that moment in time. How that `Date` instance should be presented is not in agreement, it depends heavily on what language the viewer speaks, which country they are in, what rules the country has imposed (daylight savings time), and what their cultural background has done before.

As such, a `Date` has no single associated presentation. That's why every "get the X" method on `Date` is deprecated (where X is day, month, hour, year, etc.), with the exception of grabbing the milliseconds from the 0 date (known as the epoch).

So, for every `Date` that is to be properly presented, you need to convert it to a `String` using rules that are specific to the language, country, time zone, and cultural precedent. The object that understands these rules and applies them is the `DateFormat`.

Which means, once you get the `Date` you don't need to reformat it and re-parse it to get the "right" `Date` as the two dates should be the same (for the same locale).

Problem

So i have `String trDate="20120106";` and want to get `Date trDate=2012-01-06` and am trying to use `SimpleDateFormat` for changing the pattern but first i get to `parse` the string and then generate date and then try to call `format` which gives me back string but i need date, any suggestions, here is the code i have: ``` String trDate="20120106"; Date tradeDate = new SimpleDateFormat("yyyymmdd", Locale.ENGLISH).parse(trDate); String krwtrDate = new SimpleDateFormat("yyyy-mm-dd", Locale.ENGLISH).format(tradeDate); Date krwTradeDate = new SimpleDateFormat("yyyy-mm-dd", Locale.ENGLISH).parse(krwtrDate); ``` Here is similar question but it does not answer my question I need converted string in `Date format` only because i need to pass it to another function that expects `Date object` only. Would really appreciate if someone can give example of how to get `date` in `yyyy-mm-dd` format from `string` which is in `yyyymmdd` format?

Original source

Related problems