当前位置:网站首页>Read Day6 30 minutes before going to bed every day_ Day6_ Date_ Calendar_ LocalDate_ TimeStamp_ LocalTime
Read Day6 30 minutes before going to bed every day_ Day6_ Date_ Calendar_ LocalDate_ TimeStamp_ LocalTime
2022-07-02 09:33:00 【Janson666】
1. Date introduction
stay Java in The time origin is set to The time zone of the prime meridian 1970 year 1 month 1 Midnight of the day . From the origin , Every day according to 86400 Seconds forward or back .Java Get time in API Yes Date class ,System.currentTimeMillis(),Timestamp,LocalDate,LocalTime, Let's make an introduction .
- Date format settings are DateFormat Subclasses of SimpleDateFormat Set it up , Such as new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”)
2. util Under the Date class
- The acquisition time is milliseconds , You can follow the format you need , Output after turning , use SimpleDateFormat() Class to format .
//1. util Under the Date class
Date date = new Date();
long time = date.getTime();
// Date formatting , The format is :yyyy-MM-dd HH:mm:ss
// Before formatting :1651110193459 Formatted results : 2022-04-28 09:43:13
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
String resultTime = dateFormat.format(time);
System.out.println("util Under bag Date class " + time);
System.out.println("util Under bag Date class ,format After :" + resultTime);
// Date resolution
Date parse = dateFormat.parse("2022-2-1 12:58:36");
System.out.println("parse Date resolved into ms : " + parse.getTime());

3. System The way to go
//2. System The way to go ,
// * @return the difference, measured in milliseconds, between
// * the current time and midnight, January 1, 1970 UTC.
// * @see java.util.Date
// */
// millisecond
long timeMillis = System.currentTimeMillis();
// nanosecond
long nanoTime = System.nanoTime();
System.out.println("System.currentTimeMillis() Get date :"+ timeMillis);
String timeMillisFormat = dateFormat.format(timeMillis);
System.out.println("timeMillisFormat After :" + timeMillisFormat);
System.out.println("nanoTime nanosecond :" + nanoTime);

4. Timestamp( Belong to sql package ) Is used to util The time obtained is converted to Sql Time that can be stored , And java.sql.Date Class functions are similar to . The former shows the date , Minutes and seconds , The latter only shows the date .
//3. Timestamp, It is specially used for the date format stored in the database , Get the date in the system ,
// On initialization , The time parameter needs to be passed in
// Specific date , Hours, minutes and seconds can be displayed
Timestamp timestamp = new Timestamp(new Date().getTime());
System.out.println(" be used for sql Of timeStamp timestamp :" + timestamp);
// No display Minutes and seconds , Only the date of the year is displayed
java.sql.Date date1 = new java.sql.Date(new Date().getTime());
System.out.println("java.sql.Date : " + date1);

5. Calendar
// 4. Calendar
Calendar calendar = new GregorianCalendar();
Date time2 = calendar.getTime();
System.out.println("Calendar Medium time2 " + time2);
Instant now = Instant.now();
System.out.println("Instant " + now);

6.LocalDate & LocalTime(LocalDate There are some ways getDayOfMonth();getDayOfWeek();getDayOfYear();LocalTime Method :getMinute();getHour();)
/** * LocalDate Get local date , Specific date * LocalTime Get local date , Minutes and seconds */
//localDate The purpose of the method in , Get today is the day of the year , The day in mid January , The day of the week
LocalDate now1 = LocalDate.now();
System.out.println("LocalDate " + now1);
int dayOfMonth = now1.getDayOfMonth();
System.out.println("dayOfMonth "+ dayOfMonth);
DayOfWeek dayOfWeek = now1.getDayOfWeek();
System.out.println("DayOfWeek " + dayOfWeek);
int dayOfYear = now1.getDayOfYear();
System.out.println("dayOfYear " + dayOfYear);
LocalTime now2 = LocalTime.now();
System.out.println("LocalTime " + now2);
int minute = now2.getMinute();
System.out.println("minute "+minute);
int hour = now2.getHour();
System.out.println("hour " + hour);
// Time Add date
LocalDateTime now3 = LocalDateTime.now();
System.out.println("LocalDateTime "+now3);

7. The second method of date formatting : DateTimeFormatter
// Date formatting DateTimeFormatter
// Date formatting DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = dateTimeFormatter.format(dateTime);
System.out.println("DateTimeFormatter :" + format);
// Date resolution
TemporalAccessor parse1 = dateTimeFormatter.parse("2021-05-04 07:56:24");
System.out.println("TemporalAccessor " +parse1 );
Full code display
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/** * @Author Janson * @Date 2022/4/28 9:18 * @Version 1.0 */
public class Day6_Date_Calendar_Time_TimeStamp {
public static void main(String[] args) throws ParseException {
//1. util Under the Date class
Date date = new Date();
long time = date.getTime();
// Date formatting , The format is :yyyy-MM-dd HH:mm:ss
// Before formatting :1651110193459 Formatted results : 2022-04-28 09:43:13
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String resultTime = dateFormat.format(time);
System.out.println("util Under bag Date class " + time);
System.out.println("util Under bag Date class ,format After :" + resultTime);
// Date resolution
Date parse = dateFormat.parse("2022-2-1 12:58:36");
System.out.println("parse Date resolved into ms : " + parse.getTime());
//2. System The way to go ,
// * @return the difference, measured in milliseconds, between
// * the current time and midnight, January 1, 1970 UTC.
// * @see java.util.Date
// */
// millisecond
long timeMillis = System.currentTimeMillis();
// nanosecond
long nanoTime = System.nanoTime();
System.out.println("System.currentTimeMillis() Get date :"+ timeMillis);
String timeMillisFormat = dateFormat.format(timeMillis);
System.out.println("timeMillisFormat After :" + timeMillisFormat);
System.out.println("nanoTime nanosecond :" + nanoTime);
//3. Timestamp, It is specially used for the date format stored in the database , Get the date in the system ,
// On initialization , The time parameter needs to be passed in
// Specific date , Hours, minutes and seconds can be displayed
Timestamp timestamp = new Timestamp(new Date().getTime());
System.out.println(" be used for sql Of timeStamp timestamp :" + timestamp);
// No display Minutes and seconds , Only the date of the year is displayed
java.sql.Date date1 = new java.sql.Date(new Date().getTime());
System.out.println("java.sql.Date : " + date1);
// 4. Calendar
Calendar calendar = new GregorianCalendar();
Date time2 = calendar.getTime();
System.out.println("Calendar Medium time2 " + time2);
Instant now = Instant.now();
System.out.println("Instant " + now);
/** * LocalDate Get local date , Specific date * LocalTime Get local date , Minutes and seconds */
//localDate The purpose of the method in , Get today is the day of the year , The day in mid January , The day of the week
LocalDate now1 = LocalDate.now();
System.out.println("LocalDate " + now1);
int dayOfMonth = now1.getDayOfMonth();
System.out.println("dayOfMonth "+ dayOfMonth);
DayOfWeek dayOfWeek = now1.getDayOfWeek();
System.out.println("DayOfWeek " + dayOfWeek);
int dayOfYear = now1.getDayOfYear();
System.out.println("dayOfYear " + dayOfYear);
LocalTime now2 = LocalTime.now();
System.out.println("LocalTime " + now2);
int minute = now2.getMinute();
System.out.println("minute "+minute);
int hour = now2.getHour();
System.out.println("hour " + hour);
}
}
边栏推荐
- Matplotlib swordsman line - layout guide and multi map implementation (Updated)
- 互联网API接口幂等设计
- Knife4j 2.X版本文件上传无选择文件控件问题解决
- ClassFile - Attributes - Code
- DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!
- Matplotlib swordsman Tour - an artist tutorial to accommodate all rivers
- Programmers with ten years of development experience tell you, what core competitiveness do you lack?
- C语言之二进制与十进制
- "Interview high frequency question" is 1.5/5 difficult, and the classic "prefix and + dichotomy" application question
- Elastic Stack之Beats(Filebeat、Metricbeat)、Kibana、Logstash教程
猜你喜欢

Matplotlib swordsman - a stylist who can draw without tools and code

微服务实战|负载均衡组件及源码分析

Bold prediction: it will become the core player of 5g

Timed thread pool implements request merging

Operation and application of stack and queue

Mysql 多列IN操作

Difference between redis serialization genericjackson2jsonredisserializer and jackson2jsonredisserializer

Fragmenttabhost implements the interface of housing loan calculator

Dix ans d'expérience dans le développement de programmeurs vous disent quelles compétences de base vous manquez encore?

Matplotlib剑客行——布局指南与多图实现(更新)
随机推荐
JVM instruction mnemonic
Machine learning practice: is Mermaid a love movie or an action movie? KNN announces the answer
[go practical basis] how to customize and use a middleware in gin
Solutions to Chinese garbled code in CMD window
Chrome视频下载插件–Video Downloader for Chrome
数构(C语言)——第四章、矩阵的压缩存储(下)
From concept to method, the statistical learning method -- Chapter 3, k-nearest neighbor method
BugkuCTF-web16(备份是个好习惯)
Typeerror: X () got multiple values for argument 'y‘
Redis sorted set data type API and application scenario analysis
Microservice practice | declarative service invocation openfeign practice
Knowledge points are very detailed (code is annotated) number structure (C language) -- Chapter 3, stack and queue
Insight into cloud native | microservices and microservice architecture
十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?
Double non undergraduate students enter the factory, while I am still quietly climbing trees at the bottom (Part 1)
[go practical basis] how to set the route in gin
Oracle modifies tablespace names and data files
Learn combinelatest through a practical example
Ora-12514 problem solving method
Chrome user script manager tempermonkey monkey