Examples of Java processing time

Formatting time (the SimpleDateFormat)

The following example demonstrates how to use the format SimpleDateFormat class (date) Method for formatting the time

import java.text.SimpleDateFormat;
import java.util.Date;
 
public class Main{
    public static void main(String[] args){
        Date date = new Date();
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        System.out.println(sdf.format(date));
    }
}
View Code

Output code is run results:

2015-03-27 21:13:23
View Code

Get the current time

The following example demonstrates how to use the format Date class and class SimpleDateFormat (date) method to output the current time:

Import the java.text.SimpleDateFormat;
 Import java.util.Date; 
 
public  class the Main {
     public  static  void main (String [] args) { 
        
        the SimpleDateFormat SDF = new new the SimpleDateFormat (); // formatting time 
        sdf.applyPattern ( "yyyy-MM HH -dd: mm: SS a "); // a marked am / pm of   
        a Date DATE = new new a Date (); // get the current time 
        System.out.println (" time now: "+ sdf.format (date )); // time now formatted output (24-hour) 
    } 
}
View Code

Output code is run results:

The time now: 2015-03-27 21:27:28 PM
View Code

Gets the year, month, etc.

The following example demonstrates how to use the Calendar class to output the year, month, and so on:

import java.util.Calendar;
 
public class Main {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        int day = cal.get(Calendar.DATE);
        int month = cal.get(Calendar.MONTH) + 1;
        int year = cal.get(Calendar.YEAR);
        int dow = cal.get(Calendar.DAY_OF_WEEK);
        int dom = cal.get(Calendar.DAY_OF_MONTH);
        int doy = cal.get(Calendar.DAY_OF_YEAR);
 
        System.out.println("The current time:" + cal.getTime ()); 
        System.out.println ( "Date:" + Day); 
        System.out.println ( "month:" + month The); 
        System.out.println ( "Year: "+ year); 
        System.out.println ( " day of the week: "+ Dow);   // Sunday as first day of week output is 1, Monday output 2, and so 
        System.out.println ( "the first few days of the month:" + dom); 
        System.out.println ( "Day of the year:" + Doy); 
    } 
}
View Code

Output code is run results:

The current time: Fri Mar 27 21:44:15 CST 2015 
Date: 27 
Month: 3 
Year: 2015 
day of the week: 6 
first few days of the month: 27 
day of the year: 86
View Code

Timestamps converted into time

The following example demonstrates how to use the format SimpleDateFormat class () method to be converted into time stamp.

The date and time mode (note the capitalization, meaning represent different):

  • yyyy: Year
  • MM: month
  • dd: day
  • hh: 1 ~ 12 hour (1-12)
  • HH: 24 hour (0-23)
  • mm: minutes
  • ss: seconds
  • S: ms
  • E: day of the week
  • The first day of the year: D
  • F: The first few weeks of the month (number of days this month will total over the divided 7)
  • w: the first few weeks of the year
  • W: The first few weeks of the month (will be counted according to the actual situation)
  • a: on the afternoon of identity
  • k: HH and almost one day represents a 24-hour (1-24)
  • K: hh and almost 12 hours a day express system (0-11)
  • z: the time zone
Import the java.text.SimpleDateFormat;
 Import java.util.Date; 
 
public  class the Main {
     public  static  void main (String [] args) { 
        Long timeStamp = System.currentTimeMillis ();   // get the current time stamp 
        the SimpleDateFormat SDF = new new the SimpleDateFormat ( "the mM-dd-YYYY HH: mm: SS" ); 
        String SD = sdf.format ( new new a Date (Long.parseLong (String.valueOf (timeStamp))));       // time stamp is converted into time 
        System.out.println ( "format results:" + SD); 
 
        the SimpleDateFormat SDF2 = new newSimpleDateFormat ( "dd day when HH yyyy, MM month mm minutes ss seconds" ); 
        String SD2 = sdf2.format ( new new a Date (Long.parseLong (String.valueOf (timeStamp)))); 
        System.out.println ( "format of results: "+ SD2 are); 
   } 
}
View Code

Output code is run results:

Format Results: 2018-07-10 12:17:34 
formatted results: at 12:17:34 on July 10th, 2018
View Code

 

Guess you like

Origin www.cnblogs.com/Mr-Feng/p/11372559.html