convert date to string code example
Example 1: excel date to string conversion
'Excel formula to turn a date/time into text:
=TEXT(A1,"DD/MM/YYYY hh:mm:ss AM/PM")
'Reference:
' https://support.office.com/en-us/article/text-function-20d5ac4d-7b94-49fd-bb38-93d29371225c
'---------------------------------------------------------------------------------------------
'VBA function to do the same thing:
Function DateToText$(dbl#)
DateToText = Format(dbl, "DD/MM/YYYY hh:mm:ss AM/PM")
End Function
Example 2: date to string java
Date date = Calendar.getInstance().getTime();
DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
String strDate = dateFormat.format(date);
Example 3: 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);
}
}
Example 4: java date to string
Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = formatter.format(date);
Example 5: from date to string
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
Date date = new Date();
String dateTime = dateFormat.format(date);
System.out.println("Current Date Time : " + dateTime);
} catch (ParseException e) {
e.printStackTrace();
}