当前位置:网站首页>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);
}
}
边栏推荐
- In depth analysis of how the JVM executes Hello World
- "Redis source code series" learning and thinking about source code reading
- [go practical basis] gin efficient artifact, how to bind parameters to structures
- Solutions to Chinese garbled code in CMD window
- Micro service practice | introduction and practice of zuul, a micro service gateway
- 微服务实战|手把手教你开发负载均衡组件
- VIM operation command Encyclopedia
- 洞见云原生|微服务及微服务架构浅析
- Oracle delete tablespace and user
- 长篇总结(代码有注释)数构(C语言)——第四章、串(上)
猜你喜欢

Troubleshooting and handling of an online problem caused by redis zadd

微服务实战|Eureka注册中心及集群搭建

Insight into cloud native | microservices and microservice architecture

Mysql默认事务隔离级别及行锁

Fragmenttabhost implements the interface of housing loan calculator

Redis 序列化 GenericJackson2JsonRedisSerializer和Jackson2JsonRedisSerializer的区别

The channel cannot be viewed when the queue manager is running

微服务实战|手把手教你开发负载均衡组件

Hystrix implements request consolidation

hystrix 实现请求合并
随机推荐
双非本科生进大厂,而我还在底层默默地爬树(上)
Microservice practice | declarative service invocation openfeign practice
[go practical basis] how can gin get the request parameters of get and post
西瓜书--第六章.支持向量机(SVM)
Chrome video download Plug-in – video downloader for Chrome
知识点很细(代码有注释)数构(C语言)——第三章、栈和队列
Oracle modify database character set
Redis installation and deployment (windows/linux)
自定义Redis连接池
What are the waiting methods of selenium
idea查看字节码配置
大学生四六级作文模板(自创版,成功跨过六级)
hystrix 实现请求合并
What is the function of laravel facade
C语言之最小数
记录下对游戏主机配置的个人理解与心得
Chrome user script manager tempermonkey monkey
Activity的创建和跳转
《统计学习方法》——第五章、决策树模型与学习(上)
微服务实战|手把手教你开发负载均衡组件