- java.lang.System class
public static long currentTimeMillis System class provides () returns the current time at 0:00 on January 1, 1970 0 seconds milliseconds time difference.
This method is adapted to calculate the time difference.
public class Test {
public static void main(String[] args) {
System.out.println(System.currentTimeMillis());
}
}
- java.util.Date class
represents a specific moment, with millisecond precision
Constructor:
- Date (): use the Object constructor to create a no-argument can obtain the current local time.
- Date (long date)
commonly used methods:
- getTime (): returns since January 1, 1970 00:00:00 GMT This Date object
number of milliseconds. - toString (): put this Date object is converted to the form of String: dow Mon dd
HH: mm: SS ZZZ YYYY wherein: dow week in a day (the Sun, Mon, Tue,
Wed, Thu, Fri, Sat), zzz is the time standard.
Many other methods are outdated.
public static void test2(){
//构造器一 date() 创建了一个对应当前时间的Date()对象
Date date1=new Date();
System.out.println(date1.toString());//Fri Feb 21 22:59:14 CST 2020
System.out.println(date1.getTime());
//构造器二 创建指定毫秒数的date对象
Date date2 = new Date(1582298162369L);
System.out.println(date2.toString());
//在用Date设置一个时间是会发生偏移量 并且它的月份是从0开始的
Date date1=new Date(2020,9,8);//Fri Oct 08 00:00:00 CST 3920
//所以要想获得真确时间必须在年份上减去一个1900并在月份上减去1
Date date1=new Date(2020-1900,9-1,8);//当然这个方法也早已经过时
System.out.println(date1);
}
- java.sql.Date class
Corresponds to the date of the database type variable
//创建java.sql.date对象
java.sql.Date date3 = new java.sql.Date(1582298478304L);
System.out.println(date3);//只显示年月日
//如何将java.util.Date对象转换为sql.Date对象
//情况一 :强转
Date date4=new java.sql.Date(1582299029103L);
java.sql.Date date5=(java.sql.Date)date4;
//情况二
Date date6=new Date();
java.sql.Date date7=new java.sql.Date(date6.getTime());
- java.text.SimpleDateFormat类
Date class international API not easily, largely abandoned, the java.text.SimpleDateFormat
class is not associated with a locale-specific manner to format and parsed class date.
It allows for formatting : Date -----> text, analytical: text -----> Date
Format:
- SimpleDateFormat (): the default mode and locale create objects
- public SimpleDateFormat (String pattern): This constructor parameter pattern can be used
to create an object of a specified format, which calls:
public String format (a Date DATE): Formatting Object Time DATE
l resolved:
public a Date the parse (String Source): from a given start parsing the text string to generate
a date.
public static void test() throws ParseException {
//实例化 使用默认构造器
SimpleDateFormat sdf = new SimpleDateFormat();
//格式化 日期--->字符串
Date date = new Date();
// System.out.println(date);
String format=sdf.format(date);
System.out.println(format);
//解析
String str="2020/2/22 下午1:14";//默认格式
Date parse = sdf.parse(str);
System.out.println(parse);
}
If you want to format the specified way can be called with argument constructor
public static void test() throws ParseException {
Date date = new Date();
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format2 = sdf2.format(date);
System.out.println(format2);//2020-02-22 01:23:15
//将得到的字符串进行解析
Date date1 = sdf2.parse(format2);
System.out.println(date1);
}
date format specified herein will yyyy-MM-dd hh: ss printed parsing parses the same string can pass this format: mm
- java.util.Calendar (Calendar) class
Calendar is an abstract base class, for performing primary functions between each operation date fields.
acquisition method Calendar instance:
- Use Calendar.getInstance () method
- Call it a subclass of GregorianCalendar constructor.
Calendar instance of a system of abstract representation of the time, wanted to get through get (int field) method
time information want. For example YEAR, MONTH, DAY_OF_WEEK, HOUR_OF_DAY,
MINUTE, SECOND,
static void test(){
//初始化
//方式一:创建其子类 GregorianCalendar对象
//方式二调用静态方法
Calendar instance = Calendar.getInstance();
//常用方法
//get()
int day = instance.get(Calendar.DAY_OF_MONTH);//当前时间在这个月是第几天
System.out.println(day);
System.out.println(instance.get(Calendar.DAY_OF_YEAR));
//set()
instance.set(Calendar.DAY_OF_MONTH,23);//修改了原本的instance值
day = instance.get(Calendar.DAY_OF_MONTH);
System.out.println(day);
//add()
instance.add(Calendar.DAY_OF_MONTH,3);//在原本的值上增加3天
day = instance.get(Calendar.DAY_OF_MONTH);
System.out.println(day);
//getTime():日历类---->Date
Date time = instance.getTime();//返回一个当前时间的Date对象
System.out.println(time);
//setTime Date----->日历类
Date date=new Date();
instance.setTime(date);//Date转货为日历类
day = instance.get(Calendar.DAY_OF_MONTH);//在获取这个date时间是当前月的第几天
System.out.println(day);
}
Note:
- Getting Month: January is 0, February is 1, and so on, December 11
- When acquiring a week: Sunday is 1, Tuesday is 2. . . . Saturday is 7