Java Date Processing Class

Posted by ltoso on Sat, 29 Jun 2019 21:26:04 +0200

Date Processing Class

Date class

  • Current date and time java.util.Date
import java.util.Date;

public class TestDemo {
    public static void main(String [] args) {
        Date date = new Date();
        System.out.println(date);
    }
}
  • Operation results:
Sat Jun 29 21:56:04 CST 2019

The above results represent the current date and time, which is in the international standard format.

Of course, we can use other formats to output date and time.

Construction method

  • Parametric structure: public Date()

  • Parametric structure: public Date(long date)
    • Receiving long data is equivalent to converting long data to Date
  • Convert to long: public long getTime()
    • Converting Date data to long data for easy formatting and output processing

system.currentTimeMillis(): Gets the current date and time and returns it in long form

import java.util.Date;

public class TestDemo {
    public static void main(String [] args) {
        long time = System.currentTimeMillis();// get system time        
        Date date = new Date( time );
        System.out.println(date);
        System.out.println(date.getTime());
    }
}
  • Operation results:
Sat Jun 29 22:07:16 CST 2019
1561817236726

Date formatting*

SimpleDateFormat *

  • Package: java.text.SimpleDateFormat
    • Packages specializing in international development
    • SimeDateFormat is a class that specializes in date format and converts it to String display
  • Constructing method*:
    • public SimpleDateFormat(String pattern)
  • Convert Date to String*:
    • public final String format( Date date )
  • Convert String to Date*:
    • public Date parse(String source) throws ParseException

Common conversion format units

Company Abbreviation (use) Express
year yyyy year
month MM month
day dd Day in month
time HH Hour in day(0~23)
branch mm Minute in hour
second ss Second in minute
Millisecond S Millisecond

Example

Conversion of date format

  • Converting Date data to String data
import java.text.SimpleDateFormat;
import java.util.Date;

public class TestDemo {
    public static void main(String [] args) {
        Date date = new Date() ; 
        SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS");
        String str = SDF.format(date);
        // format: Change Date data into String data
        System.out.println(str);
    }
}
  • Operation results:
2019-06-29 22:24:50:777

The function of the program is simply stated as follows:

First, Date gets the time object, formats the date through the SimpleDateFormat class, and then converts the date format to output String by format().

String to date

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TestDemo {
    public static void main(String [] args) throws ParseException {
        String str = "2012-12-25 00:00:00:000" ;
        SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS");
        Date date = SDF.parse(str) ; //Convert SDF(String) to Date
        System.out.println(date);
    }
}
  • Operation results:
Tue Dec 25 00:00:00 CST 2012

Summary:

  • The transformation between Date and String classes depends on: SimpleDateFormat

  • Conversion between String and basic data classes depends on: String. valueOf()

  • long and Date transformations rely on constructs provided by the Date class and getTime()

Calendar class

  • Date calculation

  • Calendar is an abstract class

import java.text.ParseException;
import java.util.Calendar;

public class TestDemo {
    public static void main(String [] args) throws ParseException {
        Calendar cal = Calendar.getInstance(); // Get this class of objects
        StringBuffer buf = new StringBuffer();
        // buf append year, month, day, time, seconds, milliseconds in turn
        buf.append(cal.get(Calendar.YEAR)).append("-");
        buf.append(cal.get(Calendar.MONTH) + 1).append("-");
        buf.append(cal.get(Calendar.DAY_OF_MONTH)).append("  ");
        buf.append(cal.get(Calendar.HOUR_OF_DAY)).append(":");
        buf.append(cal.get(Calendar.MINUTE)).append(":");
        buf.append(cal.get(Calendar.SECOND)).append(":");
        buf.append(cal.get(Calendar.MILLISECOND));
        // You can calculate the date and time in append
        System.out.println(buf);
    }
}

summary

  • Date data in the database is represented by java.uitl.Date
  • Code model: SimpleDateFormat class implements the conversion of String and Date data

Topics: PHP Java Database