Answers for "how to get current datetime in java and convert to string"

7

date to string java

Date date = Calendar.getInstance().getTime();  
    DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");  
    String strDate = dateFormat.format(date);
Posted by: Guest on April-24-2020
2

Java convert date to string

// java convert date to string
import java.util.Date;
import java.text.SimpleDateFormat;
public class DateToStringDemo
{
   public static void main(String[] args)
   {
      Date dt = new Date();
      SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
      String strDate = sdf.format(dt);
      System.out.println("Date format - MM/dd/yyyy to string is : " + strDate);
      sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
      strDate = sdf.format(dt);
      System.out.println("Date format - dd-M-yyyy hh:mm:ss to string is : " + strDate);
      sdf = new SimpleDateFormat("dd MMMM yyyy");
      strDate = sdf.format(dt);
      System.out.println("Date format - dd MMMM yyyy to string is : " + strDate);
      sdf = new SimpleDateFormat("dd MMMM yyyy zzzz");
      strDate = sdf.format(dt);
      System.out.println("Date format - dd MMMM yyyy zzzz to string is : " + strDate);
      sdf = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
      strDate = sdf.format(dt);
      System.out.println("Date format - E, dd MMM yyyy HH:mm:ss z to string is : " + strDate);
   }
}
Posted by: Guest on November-30-2020

Code answers related to "how to get current datetime in java and convert to string"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language