当前位置:网站首页>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);
}
}
边栏推荐
- Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedd
- zk配置中心---Config Toolkit配置与使用
- Fragmenttabhost implements the interface of housing loan calculator
- 一篇详解带你再次重现《统计学习方法》——第二章、感知机模型
- 道阻且长,行则将至
- Number structure (C language) -- Chapter 4, compressed storage of matrices (Part 2)
- 定时线程池实现请求合并
- From concept to method, the statistical learning method -- Chapter 3, k-nearest neighbor method
- Actual combat of microservices | discovery and invocation of original ecosystem implementation services
- Alibaba /热门json解析开源项目 fastjson2
猜你喜欢
西瓜书--第六章.支持向量机(SVM)
Learn combinelatest through a practical example
Enterprise level SaaS CRM implementation
西瓜书--第五章.神经网络
DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!
Microservice practice | declarative service invocation openfeign practice
自定义Redis连接池
十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?
JDBC回顾
Micro service practice | introduction and practice of zuul, a micro service gateway
随机推荐
Idea view bytecode configuration
MySQL multi column in operation
Microservice practice | teach you to develop load balancing components hand in hand
Enterprise level SaaS CRM implementation
Chrome用户脚本管理器-Tampermonkey 油猴
Dix ans d'expérience dans le développement de programmeurs vous disent quelles compétences de base vous manquez encore?
每天睡觉前30分钟阅读_day3_Files
自定義Redis連接池
JDBC回顾
zk配置中心---Config Toolkit配置与使用
Redis sorted set data type API and application scenario analysis
Solutions to Chinese garbled code in CMD window
数构(C语言)——第四章、矩阵的压缩存储(下)
Talk about the secret of high performance of message queue -- zero copy technology
AMQ 4043 solution for errors when using IBM MQ remote connection
Probability is not yet. Look at statistical learning methods -- Chapter 4, naive Bayesian method
Timed thread pool implements request merging
View the port of the application published by was
Amq6126 problem solving ideas
微服务实战|Eureka注册中心及集群搭建