Using SimpleDateFormat to format date in Java
SimpleDateFormat date formatting class
Example 1: Date to String
y represents the year.
M represents the month.
d representative day
H stands for 24-decimal hours
h stands for 12-decimal hours
m stands for minutes
s stands for seconds.
S stands for milliseconds
package date; import java.text.SimpleDateFormat; import java.util.Date; public class TestDate { public static void main(String[] args) { //y represents the year. //M represents the month. //d representative day //H stands for 24-decimal hours //h stands for 12-decimal hours //m stands for minutes //s stands for seconds. //S stands for milliseconds SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS" ); Date d= new Date(); String str = sdf.format(d); System.out.println("Current time passes yyyy-MM-dd HH:mm:ss SSS Formatted output: "+str); SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy-MM-dd" ); Date d1= new Date(); String str1 = sdf1.format(d1); System.out.println("Current time passed yyyy-MM-dd Formatted output: "+str1); } }
Example 2: String rotation date
The pattern (yyyyy/MM/dd HH: mm: ss) needs to be consistent with the string format, and if it is different, parse exceptions are thrown
package date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class TestDate { public static void main(String[] args) { SimpleDateFormat sdf =new SimpleDateFormat("yyyy/MM/dd HH:mm:ss" ); String str = "2016/1/5 12:12:12"; try { Date d = sdf.parse(str); System.out.printf("Character string %s Pass format yyyy/MM/dd HH:mm:ss %n Converting to a date object: %s",str,d.toString()); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Practice: Date formatting
Prepare an array of dates with a length of 9
Initialize the array with random dates between 1970 and 2000
Sort the dates in ascending order according to their time
For example, 1988-1-21 12:33:22 will be ahead of 1978-4-21 19:07:23, because it takes less time, although the date is larger.
Answer:
package date; import java.util.Date; import java.util.Scanner; import java.text.ParseException; import java.text.SimpleDateFormat; public class DateSort{ //Random Date Generation Method public static Date getRandomDate(int start,int end){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); long timeStart=0; long timeEnd=0; try { Date dateStart = sdf.parse(String.valueOf(start)); //System.out.println(dateStart); Date dateEnd = sdf.parse(String.valueOf(end)); timeEnd = dateEnd.getTime()-1; timeStart = dateStart.getTime(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } long randomTime = (long)(timeStart+(Math.random()*(timeEnd-timeStart))); //System.out.println(randomTime); return new Date(randomTime); } //Formatting Output Date String Method public static String toString(Date d,String format){ SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(d); } public static void main(String[] args) { Date[] ds = new Date[9]; //Create random date arrays // Scanner s = new Scanner(System.in); // System.out.println("Please enter the year of commencement:"); // int startYear = s.nextInt(); // System.out.println("Please enter the year of end:"); // int endYear = s.nextInt(); for (int i = 0; i < ds.length; i++) { //Initialize arrays ds[i] = getRandomDate(1900,2000); } System.out.println("Random Date Array Before Sorting:"); for (int i = 0; i < ds.length; i++) { System.out.print(toString(ds[i],"yyyy-MM-dd HH:mm:ss")+"\t"); if(i%3==2) System.out.println(); } for (int i = 0; i < ds.length; i++) { for (int j = 0; j < ds.length-1-i; j++) { String str1 = DateSort.toString(ds[j],"HHmmss"); String str2 = toString(ds[j+1],"HHmmss"); int j1 = Integer.parseInt(str1); int j2 = Integer.parseInt(str2); if (j1>j2) { Date temp = ds[j]; ds[j]=ds[j+1]; ds[j+1]=temp; } } } System.out.println("An array of sorted dates:"); for (int i = 0; i < ds.length; i++) { System.out.print(toString(ds[i],"yyyy-MM-dd HH:mm:ss")+"\t"); if(i%3==2) System.out.println(); } } }