当前位置:网站首页>每天睡前30分钟阅读Day6_Day6_Date_Calendar_LocalDate_TimeStamp_LocalTime
每天睡前30分钟阅读Day6_Day6_Date_Calendar_LocalDate_TimeStamp_LocalTime
2022-07-02 06:34:00 【Janson666】
1.日期介绍
在Java中 时间原点被设置为 本初子午线所在时区的 1970年1月1日的午夜。从原点开始,每天按照 86400秒向前或向回度量。Java中获取时间的API 有Date类,System.currentTimeMillis(),Timestamp,LocalDate,LocalTime,下边做一个介绍。
- 日期格式设置都采用 DateFormat 的子类 SimpleDateFormat 进行设置,如 new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”)
2. util下的 Date 类
- 获取时间为毫秒,可以按照自己需求的格式,转后后进行输出,用SimpleDateFormat() 类进行格式设置。
//1. util下的 Date 类
Date date = new Date();
long time = date.getTime();
//日期格式化,格式为:yyyy-MM-dd HH:mm:ss
//格式化前:1651110193459 格式化后的结果: 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包下的Date类" + time);
System.out.println("util包下的Date类,format后的:" + resultTime);
//日期解析
Date parse = dateFormat.parse("2022-2-1 12:58:36");
System.out.println("parse 日期解析成ms : " + parse.getTime());

3. System 下的方法
//2. System 下的方法,
// * @return the difference, measured in milliseconds, between
// * the current time and midnight, January 1, 1970 UTC.
// * @see java.util.Date
// */
//毫秒
long timeMillis = System.currentTimeMillis();
//纳秒
long nanoTime = System.nanoTime();
System.out.println("System.currentTimeMillis()获取日期:"+ timeMillis);
String timeMillisFormat = dateFormat.format(timeMillis);
System.out.println("timeMillisFormat后的:" + timeMillisFormat);
System.out.println("nanoTime 纳秒:" + nanoTime);

4. Timestamp(属于sql包) 用于将util获取的时间转为 Sql下可以存储的时间,与java.sql.Date 类功能类似。前者显示年月日,时分秒,后者只显示年月日。
//3. Timestamp,是专门用于存在数据库中的日期格式,将系统中的日期获取后,
//初始化时,需要传入时间参数
//年月日,时分秒均可以显示
Timestamp timestamp = new Timestamp(new Date().getTime());
System.out.println("用于sql的timeStamp timestamp :" + timestamp);
//不显示 时分秒,只显示年月日
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中的time2 " + time2);
Instant now = Instant.now();
System.out.println("Instant " + now);

6.LocalDate & LocalTime(LocalDate 有一些方法getDayOfMonth();getDayOfWeek();getDayOfYear();LocalTime方法:getMinute();getHour();)
/** * LocalDate 获取本地日期,年月日 * LocalTime 获取本地日期,时分秒 */
//localDate 中的方法的用途,获取今天是一年中的那天,一月中的那天,一周中的那天
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);
//时间 加 日期
LocalDateTime now3 = LocalDateTime.now();
System.out.println("LocalDateTime "+now3);

7. 日期格式化第二种方法: DateTimeFormatter
//日期格式化 DateTimeFormatter
//日期格式化 DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = dateTimeFormatter.format(dateTime);
System.out.println("DateTimeFormatter :" + format);
//日期解析
TemporalAccessor parse1 = dateTimeFormatter.parse("2021-05-04 07:56:24");
System.out.println("TemporalAccessor " +parse1 );
全部代码展示
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下的 Date 类
Date date = new Date();
long time = date.getTime();
//日期格式化,格式为:yyyy-MM-dd HH:mm:ss
//格式化前:1651110193459 格式化后的结果: 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包下的Date类" + time);
System.out.println("util包下的Date类,format后的:" + resultTime);
//日期解析
Date parse = dateFormat.parse("2022-2-1 12:58:36");
System.out.println("parse 日期解析成ms : " + parse.getTime());
//2. System 下的方法,
// * @return the difference, measured in milliseconds, between
// * the current time and midnight, January 1, 1970 UTC.
// * @see java.util.Date
// */
//毫秒
long timeMillis = System.currentTimeMillis();
//纳秒
long nanoTime = System.nanoTime();
System.out.println("System.currentTimeMillis()获取日期:"+ timeMillis);
String timeMillisFormat = dateFormat.format(timeMillis);
System.out.println("timeMillisFormat后的:" + timeMillisFormat);
System.out.println("nanoTime 纳秒:" + nanoTime);
//3. Timestamp,是专门用于存在数据库中的日期格式,将系统中的日期获取后,
//初始化时,需要传入时间参数
//年月日,时分秒均可以显示
Timestamp timestamp = new Timestamp(new Date().getTime());
System.out.println("用于sql的timeStamp timestamp :" + timestamp);
//不显示 时分秒,只显示年月日
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中的time2 " + time2);
Instant now = Instant.now();
System.out.println("Instant " + now);
/** * LocalDate 获取本地日期,年月日 * LocalTime 获取本地日期,时分秒 */
//localDate 中的方法的用途,获取今天是一年中的那天,一月中的那天,一周中的那天
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);
}
}
边栏推荐
- Long summary (code with comments) number structure (C language) -- Chapter 4, string (Part 1)
- Who is better for Beijing software development? How to find someone to develop system software
- 十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?
- Dix ans d'expérience dans le développement de programmeurs vous disent quelles compétences de base vous manquez encore?
- 别找了,Chrome浏览器必装插件都在这了
- Redis sorted set data type API and application scenario analysis
- 洞见云原生|微服务及微服务架构浅析
- Matplotlib swordsman line - layout guide and multi map implementation (Updated)
- Watermelon book -- Chapter 5 neural network
- What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
猜你喜欢

MySQL事务

Machine learning practice: is Mermaid a love movie or an action movie? KNN announces the answer

During MySQL installation, mysqld Exe reports that the application cannot start normally (0xc000007b)`

【Go实战基础】gin 如何绑定与使用 url 参数

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

Ora-12514 problem solving method

Chrome video download Plug-in – video downloader for Chrome

Servlet全解:继承关系、生命周期、容器和请求转发与重定向等

MySQL multi column in operation

概率还不会的快看过来《统计学习方法》——第四章、朴素贝叶斯法
随机推荐
十年开发经验的程序员告诉你,你还缺少哪些核心竞争力?
Troubleshooting and handling of an online problem caused by redis zadd
Chrome用户脚本管理器-Tampermonkey 油猴
There is a problem with MySQL installation (the service already exists)
JVM指令助记符
长篇总结(代码有注释)数构(C语言)——第四章、串(上)
双非本科生进大厂,而我还在底层默默地爬树(上)
Ora-12514 problem solving method
Break the cocoon | one article explains what is the real cloud primordial
微服务实战|原生态实现服务的发现与调用
Typeerror: X () got multiple values for argument 'y‘
【Go实战基础】gin 如何绑定与使用 url 参数
Microservice practice | declarative service invocation openfeign practice
Matplotlib swordsman line - layout guide and multi map implementation (Updated)
Oracle delete tablespace and user
十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?
Learn combinelatest through a practical example
机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案
我服了,MySQL表500W行,居然有人不做分区?
Redis sorted set data type API and application scenario analysis