Excerpt ……
System.currentTimeMillis# Returns the time elapsed since the epoch in milliseconds (long type). When you want to calculate how many seconds the process took.
Example.java
long start = System.currentTimeMillis();
//Some time-consuming process here
long end = System.currentTimeMillis();
System.out.println("The time taken for processing" + (end - start) + "msec");
Date.getTime# If written properly, the java.util.Date class. By doing new, it becomes an instance with a time field (long type).
Example.java
//Instantiate without arguments to the constructor
Date now = new Date();
System.out.println(now); //Display of current time
//If you want to retrieve the long value of this, getTime()Method
long lng = now.getTime();
//Instantiate with arguments to constructor
Date past = new Date(1316622225935L);
System.out.println(past); //Display of specified time
If you use the toString ()
method, you can get a string that is hard to read.
Example.java
Date d = new Date();
String str = d.toString()
System.out.println(str);
// Thu May 09 23:31:59 JST 2019 It comes out like this.
It has the following fields. Year, month, day, hour, minute, and second. All public static final int
.
YEAR
MONTH
DAY_OF_MONTH
HOUR
MINUTE
SECOND
And it has the following methods.
getInstance()
--set ()
: If you pass 6 int types, the time will be set in Calendar.
-- get ()
: If you pass Carendar.YEAR
etc., the int set in Calendar is returned.
--setTime
: If Date type is passed, the time will be set in Calendar. The return value is void.
--getTime ()
: Returns the set time as Date type.Example.java
import java.util.Calendar;
import java.util.Date;
public class Example{
//How to create a Date instance by specifying the year, month, day, hour, minute, and second
Calendar c = Calendar.getInstance();
c.set(2019, 5, 13, 22, 37, 51); //May 13, 2019 22:37:51
Date d = c.getTime();
// set()The method can also be written like this.
c.set(Calendar.YEAR, 2019);
//How to get the year, month, day, hour, minute, second from a Date instance
Date d = new Date();
Calendar c = Calendar.getInstance();
c.setTime(d);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
}
Besides this, I have a lot of members, so there are many ways to use it ...
It stores the date in the form of a format string.
--y: year --M: Month --d: day --E: Day of the week --a / p: AM / PM --H: Hours (0-23) --K: Hours (0-12) --m: minutes --s: seconds
If you write it in the form of yyyy / MM / dd HH: mm: ss
,
It will be returned in the form of 2019/05/13 23:24:31
.
Exchange Date type and String type with format ()
method and parse ()
method.
Example.java
//Get String type date from Date type
Date d = new Day();
SimmpleDateFormat f = new SimpleDateFormt("yyyy/MM/dd HH:mm:ss");
String s = f.format(d)
//Get Date type from String type
Date d = f.format(2019/05/13 23:28:10); //May 13, 2019 23:28:10
[Introduction to Java 2nd Edition] (https://www.amazon.co.jp/%E3%82%B9%E3%83%83%E3%82%AD%E3%83%AA%E3%82%8F%E3%81%8B%E3%82%8BJava%E5%85%A5%E9%96%80-%E7%AC%AC2%E7%89%88-%E3%82%B9%E3%83%83%E3%82%AD%E3%83%AA%E3%82%B7%E3%83%AA%E3%83%BC%E3%82%BA-%E4%B8%AD%E5%B1%B1-%E6%B8%85%E5%96%AC/dp/484433638X) Pp.532-540
Recommended Posts