Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,690 questions

55,449 answers

573 users

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
...