当前位置:网站首页>每天睡前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);
}
}
边栏推荐
- Number structure (C language) -- Chapter 4, compressed storage of matrices (Part 2)
- 十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?
- VIM operation command Encyclopedia
- idea查看字节码配置
- Micro service practice | introduction and practice of zuul, a micro service gateway
- 自定义Redis连接池
- Chrome user script manager tempermonkey monkey
- 以字节跳动内部 Data Catalog 架构升级为例聊业务系统的性能优化
- How to use PHP spoole to implement millisecond scheduled tasks
- 微服务实战|手把手教你开发负载均衡组件
猜你喜欢

In depth analysis of how the JVM executes Hello World

别找了,Chrome浏览器必装插件都在这了

Jd.com interviewer asked: what is the difference between using on or where in the left join association table and conditions

Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度

Supplier selection and prequalification of Oracle project management system

Chrome用户脚本管理器-Tampermonkey 油猴

Microservice practice | fuse hytrix initial experience

Matplotlib swordsman line - first acquaintance with Matplotlib

Mysql 多列IN操作

"Redis source code series" learning and thinking about source code reading
随机推荐
Mysql 多列IN操作
CKEditor 4.10.1 上传图片提示“不正确的服务器响应” 问题解决
Break the cocoon | one article explains what is the real cloud primordial
Timed thread pool implements request merging
Discussion on improving development quality and reducing test bug rate
Cartoon rendering - average normal stroke
Chrome video download Plug-in – video downloader for Chrome
Ora-12514 problem solving method
ClassFile - Attributes - Code
What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
[staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)
DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!
Actual combat of microservices | discovery and invocation of original ecosystem implementation services
【Go实战基础】gin 如何验证请求参数
「Redis源码系列」关于源码阅读的学习与思考
Chrome user script manager tempermonkey monkey
十年开发经验的程序员告诉你,你还缺少哪些核心竞争力?
一篇详解带你再次重现《统计学习方法》——第二章、感知机模型
Pdf document of distributed service architecture: principle + Design + practice, (collect and see again)
Attributes of classfile