Java convert date to string java example
package com.w3spoint;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToString {
public static void main(String args[]){
try {
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String dateString = df.format(new Date());
System.out.println("Date: " + dateString);
} catch (Exception e) {
e.printStackTrace();
}
}
} |
package com.w3spoint; import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date; public class DateToString {
public static void main(String args[]){
try {
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String dateString = df.format(new Date());
System.out.println("Date: " + dateString);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Download this example.