当前位置:网站首页>每天睡前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);
}
}
边栏推荐
- 西瓜书--第五章.神经网络
- Cloud computing in my eyes - PAAS (platform as a service)
- Enterprise level SaaS CRM implementation
- There is a problem with MySQL installation (the service already exists)
- 微服务实战|熔断器Hystrix初体验
- I've taken it. MySQL table 500W rows, but someone doesn't partition it?
- Microservice practice | declarative service invocation openfeign practice
- MySQL error: unblock with mysqladmin flush hosts
- How to choose between efficiency and correctness of these three implementation methods of distributed locks?
- Timed thread pool implements request merging
猜你喜欢
DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!
What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度
告别996,IDEA中必装插件有哪些?
概率还不会的快看过来《统计学习方法》——第四章、朴素贝叶斯法
企业级SaaS CRM实现
Programmers with ten years of development experience tell you, what core competitiveness do you lack?
[go practical basis] how can gin get the request parameters of get and post
Learn combinelatest through a practical example
In depth analysis of how the JVM executes Hello World
随机推荐
VIM operation command Encyclopedia
Solutions to Chinese garbled code in CMD window
Oracle modify database character set
Matplotlib剑客行——初相识Matplotlib
【Go实战基础】gin 如何验证请求参数
[staff] time mark and note duration (staff time mark | full note rest | half note rest | quarter note rest | eighth note rest | sixteenth note rest | thirty second note rest)
Shengshihaotong and Guoao (Shenzhen) new energy Co., Ltd. build the charging pile industry chain
VIM操作命令大全
使用IBM MQ远程连接时报错AMQ 4043解决思路
Dix ans d'expérience dans le développement de programmeurs vous disent quelles compétences de base vous manquez encore?
微服务实战|微服务网关Zuul入门与实战
Demand delineation executive summary
Thinkphp5 how to determine whether a table exists
告别996,IDEA中必装插件有哪些?
Oracle modifies tablespace names and data files
微服务实战|声明式服务调用OpenFeign实践
【Go实战基础】gin 如何自定义和使用一个中间件
MySql报错:unblock with mysqladmin flush-hosts
[staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)
Knife4j 2.X版本文件上传无选择文件控件问题解决