[Java] Get miscellaneous dates

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.

Various methods of Calendar class

It has the following fields. Year, month, day, hour, minute, and second. All public static final int.

And it has the following methods.

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 ...

Various methods of SimpledateFormat class

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

Reference book

[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

[Java] Get miscellaneous dates
[Java] Initialize, add, get
Get EXIF information in Java
[Java] Get KClass in Java [Kotlin]
How to use Java HttpClient (Get)
[Java] Get the dates of the past Monday and Sunday in order
Get Java Silver on your commute!
Get country from IP address (Java)
Get Null-safe Map values in Java
Java
Get stuck in a Java primer
Java
Get the result of POST in Java
[Java] Get List / Map elements with Iterator
[Java] Get the day of the specific day of the week
Get caller information from stack trace (java)
[Java] How to get the current directory
[Java] Get tag information from music files
Compare Android Java dates only (excluding time)
Inexperienced learning methods to get Java Silver 11
How to get the date in java
Get history from Zabbix server in Java
Get Timestamp with Azure BlobStorage Java SDK
[Java8] Search the directory and get the file
[Java] Get the date with the LocalDateTime class