How to convert date to string in Java

5 Answers

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

public class MyClass {
    public static void main(String args[]) {
        Date date = Calendar.getInstance().getTime();  
        DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");  
        
        String s = dateFormat.format(date);  
        System.out.println(s);  
    }
}




/*
run:

2021-22-17

*/

 



answered Nov 17, 2021 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("MM/dd/yyyy");  
        
        String s = formatter.format(date);  
        
        System.out.println(s);  
    }
}




/*
run:

11/17/2021

*/

 



answered Nov 17, 2021 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");  
        
        String s = formatter.format(date);  
        
        System.out.println(s);  
    }
}




/*
run:

17-11-2021

*/

 



answered Nov 17, 2021 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");  
        
        String s = formatter.format(date);  
        
        System.out.println(s);  
    }
}




/*
run:

Wed, 17 Nov 2021

*/

 



answered Nov 17, 2021 by avibootz
0 votes
import java.text.SimpleDateFormat;  
import java.text.DateFormat;  
import java.util.Date;  
 
public class MyClass {
    public static void main(String args[]) {
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
 
        String s = dateFormat.format(date);
 
        System.out.println(s);
    }
}
 
 
 
 
/*
run:
 
05-10-2024 06:21:58
 
*/

 



answered May 10, 2024 by avibootz

Related questions

1 answer 104 views
1 answer 77 views
1 answer 152 views
1 answer 111 views
1 answer 108 views
...