Different way to print Date and Time in Java
import java.util.*;
import java.io.*;
import java.text.*;
import java.time.LocalDate;
class DateTime{
public static void main (String[] args) {
Date d = new Date();
System.out.println(d); //Mon Dec 03 10:27:49 UTC 2018
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
System.out.println(sdf.format(new Date())); //03-12-2018
LocalDate dash= java.time.LocalDate.now();
String da=dash.toString();
System.out.println(da); //2018-12-03
String months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int year;
GregorianCalendar gcalendar = new GregorianCalendar();
System.out.print("Date: ");
System.out.print(months[gcalendar.get(Calendar.MONTH)]);
System.out.print(" " + gcalendar.get(Calendar.DATE) + " ");
System.out.println(year = gcalendar.get(Calendar.YEAR));
System.out.print("Time: ");
System.out.print(gcalendar.get(Calendar.HOUR) + ":");
System.out.print(gcalendar.get(Calendar.MINUTE) + ":");
System.out.println(gcalendar.get(Calendar.SECOND));
}
}
Comments
Post a Comment