Java 日历 setTimeInMillis()方法及示例

Java 日历 setTimeInMillis()方法及示例

Calendar类中的 setTimeInMillis(long mill_sec) 方法用于从传递的长值中设置由该Calendar代表的Calendars时间。

语法

public void setTimeInMillis(long mill_sec)

参数: 该方法需要一个长数据类型的参数 mill_sec ,指的是要设置的日期。

返回值: 该方法不返回任何值。

下面的程序说明了日历类的setTimeInMillis()方法的工作原理:

例1 :

// Java code to illustrate
// setTimeInMillis() method
  
import java.util.*;
  
public class Calendar_Demo {
    public static void main(String args[])
    {
  
        // Creating calendar object
        Calendar calndr = Calendar.getInstance();
  
        // Getting the time in milliseconds
        System.out.println("The Current"
                           + " Time is: "
                           + calndr.getTime());
  
        // Changing time to 2000 milli-second
        calndr.setTimeInMillis(2000);
  
        // Displaying the new time
        System.out.println("After setting"
                           + " Time: "
                           + calndr.getTime());
    }
}

输出:

The Current Time is: Fri Feb 22 08:00:54 UTC 2019
After setting Time: Thu Jan 01 00:00:02 UTC 1970

例2 :

// Java code to illustrate
// setTimeInMillis() method
  
import java.util.*;
  
public class Calendar_Demo {
    public static void main(String args[])
    {
  
        // Creating calendar object
        Calendar calndr = Calendar.getInstance();
  
        // Getting the time in milliseconds
        System.out.println("The Current"
                           + " Time is: "
                           + calndr.getTime());
  
        // Changing time to 8000 milli-second
        calndr.setTimeInMillis(8000);
  
        // Displaying the new time
        System.out.println("After setting"
                           + " Time: "
                           + calndr.getTime());
    }
}

输出:

The Current Time is: Fri Feb 22 08:01:02 UTC 2019
After setting Time: Thu Jan 01 00:00:08 UTC 1970

**参考资料: **https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#setTimeInMillis(长)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Java 日历