简体   繁体   中英

java - String to Date Format

I have a problem with String conversion to Date Format. Please help me. Below is my code:

String strDate = "23/05/2012"; // Here the format of date is MM/dd/yyyy

Now i want to convert the above String to Date Format like " 23 May, 2012 ".

I am using below code but i am getting value as " Wed May 23 00:00:00 BOT 2012 "

String string = "23/05/2012";
Date date = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(string);
System.out.println(date); // Wed May 23 00:00:00 BOT 2012

How can i get the value as " 23 May, 2012 ". Please help me friends....

You must render the date again.

You have the string and you parse it correctly back to a Date object. Now, you have to render that Date object the way you want.

You can use SimpleDateFormat again, changing the pattern. Your code should then look like

String string = "23/05/2012";
Date date = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(string);
String newFormat = new SimpleDateFormat("dd MMMM, yyyy").format(date);
System.out.println(newFormat); // 23 May, 2012

Use the method format() from the class SimpleDateFormat with the correct pattern.

Simple use:

SimpleDateFormat df = new SimpleDateFormat("dd MMM, yyyy");
System.out.println(df.format(date));
import java.text.*;
import java.util.*;


public class Main {
    public static void main(String[] args) throws ParseException{
        String strDate = "23/02/2012";
        Date date = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(strDate);
        String date1 = new SimpleDateFormat("dd MMMM, yyyy").format(date);
        System.out.println(date1);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM