How to use SimpleDateFormat to format and parse date and time in Java

4 Answers

0 votes
import java.text.SimpleDateFormat;  
import java.util.Date;  

public class MyClass {
    public static void main(String args[]) {
        Date date = new Date();  
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");  
        String s = formatter.format(date);  
        
        System.out.println(s);  
    } 
}



/*
run:

23/05/2020

*/

 



answered May 23, 2020 by avibootz
0 votes
import java.text.SimpleDateFormat;  
import java.util.Date;  

public class MyClass {
    public static void main(String args[]) {
        Date date = new Date();  
        SimpleDateFormat formatter = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");  
        String s = formatter.format(date);  
        
        System.out.println(s);  
    } 
}



/*
run:

23-5-2020 07:43:07

*/

 



answered May 23, 2020 by avibootz
0 votes
import java.text.SimpleDateFormat;  
import java.util.Date;  

public class MyClass {
    public static void main(String args[]) {
        Date date = new Date();  
        SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy zzzz");  
        String s = formatter.format(date);  
        
        System.out.println(s);  
    } 
}



/*
run:

23 May 2020 Greenwich Mean Time

*/

 



answered May 23, 2020 by avibootz
0 votes
import java.text.SimpleDateFormat;  
import java.util.Date;  

public class MyClass {
    public static void main(String args[]) {
        Date date = new Date();  
        SimpleDateFormat formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");  
        String s = formatter.format(date);  
        
        System.out.println(s);  
    } 
}



/*
run:

Sat, 23 May 2020 07:44:45 GMT

*/

 



answered May 23, 2020 by avibootz
...