How do I convert the date from one format to another date object in another format without using any deprecated classes?

convert date from one format to another javascript
convert date from one format to another in excel
convert date string from one format to another in java
convert date from one format to another python
how to convert date from one format to another in sql
convert date from one format to another php
convert date from one format to another c#
how to format date from one form to another in excel

I'd like to convert a date in date1 format to a date object in date2 format.

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMM dd, yyyy");
    SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyyMMdd");
    Calendar cal = Calendar.getInstance();
    cal.set(2012, 8, 21);
    Date date = cal.getTime();
    Date date1 = simpleDateFormat.parse(date);
    Date date2 = simpleDateFormat.parse(date1);
    println date1
    println date2

Use SimpleDateFormat#format:

DateFormat originalFormat = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("yyyyMMdd");
Date date = originalFormat.parse("August 21, 2012");
String formattedDate = targetFormat.format(date);  // 20120821

Also note that parse takes a String, not a Date object, which is already parsed.

Java Date - parse, format and conversion, Date class. These use-cases are frequently required, and having them in one place will help new date, get current date, parse date to string or format Date object using java.util. Table of Contents Format Date to string Parse string to Date Get current date FYI, in java 8, you can get different time units in below fashion. 1. Your format is incorrect. Change it to "dd-MM-yyyy" 2. Why do you want conversion of dt to the desired format than back to the original format? You already have both "MM-dd-yyyy" format date as well as "dd-MM-yyyy" format date.

Parsing and Formatting (The Java™ Tutorials > Date Time , This date-time Java tutorial describes how to use the java.time APIs enhancements, and removed or deprecated options for all JDK releases. These classes also provide format methods for formatting temporal-based objects for display. the Non-ISO Date Conversion page provides another example of a date formatter. For this type of situation, you have to struggle with date formatting. Date formatting or converting one date format to another date format can be done easily using SimpleDateFormat. In this page, we will see some examples of changing the format of date form one format to another.

Since Java 8, we can achieve this as follows:

private static String convertDate(String strDate) 
    //for strdate = 2017 July 25

    DateTimeFormatter f = new DateTimeFormatterBuilder().appendPattern("yyyy MMMM dd")

    LocalDate parsedDate = LocalDate.parse(strDate, f);
    DateTimeFormatter f2 = DateTimeFormatter.ofPattern("MM/d/yyyy");

    String newDate = parsedDate.format(f2);

    return newDate;

The output will be : "07/25/2017"

Still using java.util.Date? Don't!, One of those is new Date and Time API for Java, also known as JSR-310. Date , which represents a single day (though without a timezone associated with it). its domain very precisely with classes that represent different use cases for Date and To create a formatted string out a LocalDateTime object you can use the  To output it using a specific format, you need to do the way you did: String tDate = formatter1.format(date); The reason why you think it has the "wrong format" is probably because when you try to output it, it does a toString() by default, which doesn't format it the way you want.

Please refer to the following method. It takes your date String as argument1, you need to specify the existing format of the date as argument2, and the result (expected) format as argument 3.

Refer to this link to understand various formats: Available Date Formats

public static String formatDateFromOnetoAnother(String date,String givenformat,String resultformat) {

    String result = "";
    SimpleDateFormat sdf;
    SimpleDateFormat sdf1;

    try {
        sdf = new SimpleDateFormat(givenformat);
        sdf1 = new SimpleDateFormat(resultformat);
        result = sdf1.format(sdf.parse(date));
    catch(Exception e) {
        return "";
    finally {
    return result;

DateFormatUtils (Apache Commons Lang 3.10 API), Formatting is performed using the thread-safe FastDateFormat class. Note that the JDK has a bug wherein ISO 8601 formatter for date-time without time zone. You need to use SimpleDateFormat instance and pass the date pattern you prefer.. Current pattern: yyyy-MM-dd The pattern that is needed: dd-MM-yyyy Now try the following. It produces the required pattern of date format.

Hope this will help someone.

 public static String getDate(
        String date, String currentFormat, String expectedFormat)
throws ParseException {
    // Validating if the supplied parameters is null 
    if (date == null || currentFormat == null || expectedFormat == null ) {
        return null;
    // Create SimpleDateFormat object with source string date format
    SimpleDateFormat sourceDateFormat = new SimpleDateFormat(currentFormat);
    // Parse the string into Date object
    Date dateObj = sourceDateFormat.parse(date);
    // Create SimpleDateFormat object with desired date format
    SimpleDateFormat desiredDateFormat = new SimpleDateFormat(expectedFormat);
    // Parse the date into another format
    return desiredDateFormat.format(dateObj).toString();

Joda-Time – Java date and time API - User Guide, We finish with a look at the important topic of formatting and parsing and a few more The millisecond instant can be converted to any date time field using a Chronology. Other implementations of ReadableInstant include Instant and the deprecated The DateTime class has a constructor which takes an Object as input. I am trying to write a program that asks for the user to input the date in the format mm/dd/yyyy and convert it. So, if the user input 01/01/2009, the program should display January 01, 2009. This is my program so far. I managed to convert the month, but the other elements have a bracket around them so it displays January [01] [2009].

Migrating to the New Java 8 Date Time API, The old date library included only a single time representation class LocalTime – time with nanosecond precision and without date Another advantage is that all time representations in Java 8 Date Time return a new copy instead of modifying state of the original object. Time formatting and parsing. I need to convert date string "2013-1-25" to string "1/25/13" in python. I looked at the datetime.strptime but still can't find a way for this. I assume I have import datetime before running each of the lines of code below. This prints "1/25/13". Use import datetime. – Stefan Nov 4 '18 at 13:35. If you can live with 01 for January instead of

Docs, Note: In 2.4.0, the globally exported moment object was deprecated. For version 2.5.x, in case you use other plugins that rely on Moment but are not of modifying the native Date.prototype , Moment.js creates a wrapper for the Date object. If you know the format of an input string, you can use that to parse a moment. xml returning date format is "7/8/2014 12:00:00 PM" The format specified in the question does not match the example posted in the comment. The xmlDate text is in fact in M/d/yyyy format, not MM/dd/yyyy! Using ParseExact means we are giving DateTime the exact format to expect. When the format does not match the actual string pattern, it will fail:

DateTimeFormat (GWT Javadoc), public class DateTimeFormat extends DateTimeFormat. Formats and parses dates and times using locale-sensitive patterns. Any characters in the pattern that are not in the ranges of [' a '. When numeric fields abut one another directly​, with no intervening delimiter characters, deprecated use DateTimeFormat instead  Creating a Date Object for a Particular Date: 3. Create java Date from specific time example: 4. Convert string of time to time object: 5. Convert String to Date object: 6. Convert a String to Date: 7. Check if a String is a valid date: 8. Compare two Java Date objects using after method example: 9. Compare two Java Date objects using before

  • I have created a simple method to do this. refer to
  • you'd need to wrap Date parsedDate = simpleDateFormat1.parse(formattedDate); with try catch as it'd throw parseexception.
  • What are the exact inputs you are trying to parse? Try this demo Perhaps you are passing an invalid Date?
  • My input is a string of this format August 21, 2012 and I need to save it as another string of format 20120821
  • @Phoenix: Are you setting the Locale to Locale.ENGLISH of the first SimpleDateFormat? Otherwise, if you are on a non-english Locale, August will not be parsed correctly and will throw a ParseException.
  • While this answer was the best answer at the time, I should advise visitors coming to the site now that the classes used are considered legacy these days. The answers which refer to Java 8 are now the preferred way to do this.
  • While SimpleDateFormat is not officially deprecated yet, it is certainly both long outdated and notoriously troublesome. Today we have so much better in java.time, the modern Java date and time API and its DateTimeFormatter. Yes, you can use it on Android. For older Android see How to use ThreeTenABP in Android Project.
  • I didn't knew about that. I will check it soon
  • While not officially deprecated, the classes DateFormat, SimpleDateFormat and TimeZone are poorly designed and long outdated. Please don’t suggest using them in 2019, it’s a bad idea.
  • SimpleDateFormat is limited to parsing millisecond precision, and will corrupt the minutes/seconds if asked to go beyond that.