In this tutorial, we will show you how to convert a String to java.util.Date. Many Java beginners Always find this problem of Date conversion. so we decided to post this solution !! let understand how to convert String Date in JAVA.

    // String -> Date
    SimpleDateFormat.parse(String);

    // Date -> String
    SimpleDateFormat.format(date);

java.text.SimpleDateFormat provide many date and time patterns :

LetterDescriptionExamples
yYear2013
MMonth in yearJuly, 07, 7
dDay in month1-31
EDay name in weekFriday, Sunday
aAm/pm markerAM, PM
HHour in day0-23
hHour in am/pm1-12
mMinute in hour0-60
sSecond in minute0-60

When we have a date string like : 9-Oct-2008

package com.example.demo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main{

    public static void main(String[] argv) {

        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
        String dateInString = "7-Jun-2013";

        try {

            Date date = formatter.parse(dateInString);
            System.out.println(date);
            System.out.println(formatter.format(date));

        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

}

The output will be like :

Thu Oct 09 00:00:00 CEST 2008
09-Oct-2008

When we have a date string like : 09/10/2008

package com.example.demo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Mainn {

	public static void main(String[] args) {
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        String dateInString = "09/10/2008";

        try {

            Date date = formatter.parse(dateInString);
            System.out.println(date);
            System.out.println(formatter.format(date));

        } catch (ParseException e) {
            e.printStackTrace();
        }


	}

}

The output :

Thu Oct 09 00:00:00 CEST 2008
09-Oct-2008

When we have a String Date like : Fri, June 7 2013

package com.example.demo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main{

    public static void main(String[] argv) {

        SimpleDateFormat formatter = new SimpleDateFormat("E, MMM dd yyyy");
        String dateInString = "Fri, June 7 2013";

        try {

            Date date = formatter.parse(dateInString);
            System.out.println(date);
            System.out.println(formatter.format(date));

        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

}

The output will be like :


Fri Jun 07 00:00:00 MYT 2013
Fri, Jun 07 2013

When the string Date is like : Friday, Jun 7, 2013 12:10:56 PM

package com.example.demo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main{

    public static void main(String[] argv) {

        SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a");
        String dateInString = "Friday, Jun 7, 2013 12:10:56 PM";

        try {

            Date date = formatter.parse(dateInString);
            System.out.println(date);
            System.out.println(formatter.format(date));

        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

}

the output :

Fri Jun 07 12:10:56 MYT 2013
Friday, Jun 07, 2013 12:10:56 PM

References

  1. Mkyong
  2. SimpleDateFormat JavaDoc
  3. Java 8 – How to convert String to LocalDate
  4. Stackoverflow : simpledateformat parsing date with ‘Z’ literal
  5. Wikipedia : ISO 8601
  6. Time Zone and Offset Classes
  7. GMT VS UTC
  8. What is a Time Zone?
  9. Joda Time

Also read : Convert String to int

Commentaires

  1. Pingback: Convert Instant to ZonedDateTime in JAVA » JavaTuto

Laisser un commentaire