Java String to Date Conversion in different format

36,677

--- 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.Dates 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).

Share:
36,677
Rachel
Author by

Rachel

I am here for learning and I learn different aspects of Computer Science from the Valuable Answers which I get from Stackoverflow Users. Thank you SO Community. I owe my knowledge to you.

Updated on April 21, 2020

Comments

  • Rachel
    Rachel about 4 years

    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?

  • Rachel
    Rachel about 12 years
    I agree with you but the thing is soap api call expects date in yyyy-mm-dd format and I have string format in yyyymmdd format, so I want to first convert yyyymmdd to yyyy-mm-dd format in string and then convert this string to date object using SimpleFormat parse method but right now I do not see that as an viable option.
  • adarshr
    adarshr about 12 years
    @Rachel Just convert yyyymmdd to a Date and pass to your method. Don't do any conversions yet. Once you're inside the method, then convert the Date back to the String in the format you want, yyyy-mm-dd
  • Edwin Buck
    Edwin Buck about 12 years
    In that case, you don't need to pass in a Date, you need to pass in a String. I'll update the answer (as these comment boxes are not the best place to paste code).
  • Rachel
    Rachel about 12 years
    @EdwinBuck: API does not demand string but it demands date object
  • Edwin Buck
    Edwin Buck about 12 years
    @Rachel The post has been updated, and since Dates represent a moment in time (to the nearest millisecond), m could mean minute or month. Ambiguous formats are not desirable, so Java's DateFormat defines m to mean minute, and M to mean month. I'll bet that's the reason you're not getting expected output (as your format reads "years-minutes-days")
  • Edwin Buck
    Edwin Buck about 12 years
    @Rachel Then give it a Date object. Why do you keep trying to re-format it into a String? Date objects have no default format. They have no format at all, which is the reason that after you format one, you have a String and not a Date.
  • Rachel
    Rachel about 12 years
    Yes you are right but now it appears that soap accepts yyyy-mm-dd string format and then internally it converts to date format.
  • Edwin Buck
    Edwin Buck about 12 years
    @Rachel Then give it a String. What confuses most of us is that you seem to flip-flop on what the API requests. Whatever it requests is what you must provide. Whatever it does internally is its own business. Don't micro-manage an API you haven't developed, as it probably does internal things for reasons you have not yet discovered. Yes, it sometimes pays to be curious; but, remember that APIs are designed to be used at their interface, which is the only safe way of using them.
  • Rachel
    Rachel about 12 years
    @EdwinBuck: I agree Edwin, it's just that I am not sure how api is working internally and rightly that should not be my business.
  • Edwin Buck
    Edwin Buck about 12 years
    @Rachel It's still fun to look, just don't dig so deeply that you get confused as to what the interface requests. Give it what it wants at the front door, and if you want to stroll in and start peeking around, that's fine. Just remember that whatever you find inside, it's only guaranteed to work if you go back to its front door and give it what it wants at the front door.
  • Rachel
    Rachel about 12 years
    +1 for your detailed explanation and advise, this chat has enlightened my knowledge