Home  >  Article  >  Java  >  How can I reformat dates without using deprecated classes?

How can I reformat dates without using deprecated classes?

Barbara Streisand
Barbara StreisandOriginal
2024-11-25 01:02:20767browse

How can I reformat dates without using deprecated classes?

Reformatting Dates with Simplified Methods

To convert a date from one format to another without using deprecated classes like SimpleDateFormat, follow these steps:

1. Create DateFormatter objects

DateFormat originalFormat = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("yyyyMMdd");

2. Parse the original date string

Date date = originalFormat.parse("August 21, 2012");

3. Format the date using the target format

String formattedDate = targetFormat.format(date); // 20120821

Note:

  • parse takes a String as input, not a Date object.
  • This approach avoids using deprecated classes like SimpleDateFormat, which have been superseded by newer APIs.

The above is the detailed content of How can I reformat dates without using deprecated classes?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn