Rachel
Rachel

Reputation: 103427

Java String to Date Conversion in different format

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?

Upvotes: 2

Views: 43057

Answers (4)

Kamal
Kamal

Reputation: 4078

If the String date always comes in the format you have mentioned than you can use the subString method to extract the year, month and day.

String trDate="20120106";

Using the subString method for String extract the Year, month and day

String year = "2012";
String month = "01";
String day = "06";

And parse these to integer. Assuming you will do the parsing of string to integer

int intYear = 2012;
int intMonth = 01;
int intDay = 06;

Calendar calendar= new GregorianCalendar(intYear, intMonth - 1, intDay);

Date date = new Date(calendar);

Upvotes: -2

aioobe
aioobe

Reputation: 420991

can you give me an example of how to get date in yyyy-mm-dd format from string which is in yyyymmdd format

A Date objects does not have a format associated with it. Internally it typically just stores a long value.

If some other methods is responsible for printing the date, then you unfortunately have no control over how the resulting output is formatted.

Upvotes: 0

Johanna
Johanna

Reputation: 5293

Do I understand it well?

You have a method which only accepts Date as parameter, and internally that method converts the Date to a string in the wrong format using toString()?

Best is to modify that method and exchange the use of toString() with SimpleDateFormat.format().

If you can't modify that method, than you can "cheat" with OO, i. e. replace the toString method with your own implementation, for example like this:

public class MyDate {
   private static final FMT = SimpleDateFormat("yyyy-mm-dd", Locale.ENGLISH);

   MyDate(long time) {
      super(time);
   }

   String toString() {
      return FMT.format(this);
   }
}

and you call your method with

xxx = myMethod(new MyDate(new SimpleDateFormat("yyyymmdd", Locale.ENGLISH).parse(trDate).getTime()));

But if that method does not use toString internally but SimpleDateFormat with the wrong format, then my proposition does not work.

Upvotes: 0

Edwin Buck
Edwin Buck

Reputation: 70909

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

Upvotes: 7

Related Questions