<< All Posts
Examples using Date and SimpleDateFormat in Java



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {

	public static void main(String[] args) throws ParseException 
	{	
		// Converting Date to String
		SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		Date d = new Date();
		String date = df.format(d); 
		System.out.println(date);  // 23/02/2014
		
		// Converting String to Date
        SimpleDateFormat df2 = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
        String dateInString = "23-02-2014 10:20:56";
        Date date2 = df2.parse(dateInString);
        System.out.println(date2); // Sun Feb 23 10:20:56 BRT 2014	
        
        // Getting the current DateTime
    	SimpleDateFormat df3 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    	Date date3 = new Date();
    	System.out.println(df3.format(date3)); // 2014/02/23 22:21:14
    	
	}
}

Rodolfo



<< All Posts

rodolfo.io

🇧🇷 🇨🇦
Runs on OpenBSD 🐡