Java获取时间格式(年月日时分秒毫秒)
Java中描述日期时间调用Date类型,该类型存储有时间的年月日、时分秒信息。
Date系统时间是以 1970年0时0分0秒 开始计时。
其中:
需要获取的时间单位 | 格式 |
---|---|
年 | YYYY or yyyy |
月 | MM(大写) |
日 | dd(小写) |
时 | HH or hh |
分 | mm(小写) |
秒 | ss(小写) |
毫秒 | SS(大写) |
从当年的1月1日起,到获取时间是该年份的第几天 | DD(大写) |
方法一:Calendar类
import java.util.Calendar;
import java.util.Date;public class Demo {public static void main(String[] args) {Calendar calendar = Calendar.getInstance();calendar.setTime(new Date()); // 放入Date类型数据calendar.get(Calendar.YEAR); // 获取年份calendar.get(Calendar.MONTH); // 获取月份calendar.get(Calendar.DATE); // 获取日calendar.get(Calendar.HOUR); // 时(12进制)calendar.get(Calendar.HOUR_OF_DAY); // 时(24进制)calendar.get(Calendar.MINUTE); // 分calendar.get(Calendar.SECOND); // 秒calendar.get(Calendar.MILLISECOND); // 毫秒System.out.println(calendar.get(Calendar.YEAR));System.out.println(calendar.get(Calendar.MONTH));System.out.println(calendar.get(Calendar.DATE));System.out.println(calendar.get(Calendar.HOUR));System.out.println(calendar.get(Calendar.HOUR_OF_DAY));System.out.println(calendar.get(Calendar.MINUTE));System.out.println(calendar.get(Calendar.SECOND));System.out.println(calendar.get(Calendar.MILLISECOND));}
}
方法二:SimpleDateFormat类
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;public class Demo {public static void main(String[] args) {File file = new File("D:/time/test.txt");// 获取在D盘下time文件夹里面test.txt最后一次修改时间long lastModified = file.lastModified();// 此处显示单位秒(1970年1月1日为绝对时间时刻,到文件修改的最后时刻共经过多少秒)System.out.println(lastModified);// 获取年份SimpleDateFormat year = new SimpleDateFormat("YYYY"); String format1 = year.format(new Date(lastModified));// 获取月份SimpleDateFormat month = new SimpleDateFormat("MM"); String format2 = month.format(new Date(lastModified));// 获取日SimpleDateFormat date = new SimpleDateFormat("dd"); String format3 = date.format(new Date(lastModified));// 时SimpleDateFormat hour = new SimpleDateFormat("HH"); String format4 = hour.format(new Date(lastModified));// 分SimpleDateFormat minute = new SimpleDateFormat("mm"); String format5 = minute.format(new Date(lastModified));// 秒SimpleDateFormat second = new SimpleDateFormat("ss"); String format6 = second.format(new Date(lastModified));// 毫秒SimpleDateFormat milliSecond = new SimpleDateFormat("SS"); String format7 = milliSecond.format(new Date(lastModified));//该年份从1月1日为绝对是件,到文件修改的最后时刻共经过多少天SimpleDateFormat numberDay = new SimpleDateFormat("DD"); String format8 = numberDay.format(new Date(lastModified));System.out.println(format1);System.out.println(format2);System.out.println(format3);System.out.println(format4);System.out.println(format5);System.out.println(format6);System.out.println(format7);System.out.println(format8);}
}
Java获取时间格式(年月日时分秒毫秒)相关推荐
- 时间格式 年月日时分秒毫秒
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS"); String time = for ...
- Android 时间格式 年月日时分秒毫秒
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS"); String time = ...
- java时间格式年月日时分秒_java日期格式(年月日时分秒毫秒)
java日期格式(年月日时分秒毫秒) 2020-08-13 07:09 阅读数 79 package test.remote.tools.combine; import java.text.Simpl ...
- c#.net 获取时间日期年月日时分秒生成自动文件名格式
下面是日期和时间的各种方法,转换为字符串. 如果把输出的格式改下就可以做类似的文件名了,例如:2016010110101224356.doc c#用DateTime.Now.ToString(&qu ...
- c#.net 获取时间日期年月日时分秒格式(转)
今天写代码发现两个比较不错的分享下: 1.DateTime.ParseExact 很多时候我们获取的时间是数字形式表示的,好比20140127134015.927856,通过这个方法 DateTime ...
- android获取年月日时分秒毫秒,Android获取两个日期其间间隔的天数
/** * 获取两个日期之间的间隔天数 * @return */ public static int getGapCount(Date startDate, Date endDate) { Calen ...
- JAVA 获取当前时间(年月日时分秒)
LocalDateTime单独获取年月日和时分秒 LocalDateTime localDateTime = LocalDateTime.now(); localDateTime.format(Dat ...
- php获取当前时间的年月日时分秒格式
php获取当前时间的年月日时分秒 解决方法: $now_time= time(); 年月日时分秒格式:http://www.yayihouse.com/yayishuwu/chapter/1727
- JAVA 计算年月日时分秒毫秒
/**计算年月日时分秒毫秒**/ public static String Millisecond(String begins,String ends,String appointmentNo,Str ...
- python 获取当前时间(年月日时分秒)
python 获取当前时间(年月日时分秒) now_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
最新文章
- delphi下的MVC架构-eMVC
- 硬件:RS232基础知识笔记
- 高通8155车载芯片_WEY来“摩卡”云首秀,搭载高通8155芯片
- Exchange2003-2010迁移系列之二,迁移前的准备工作(上)
- advanced east_SpriteKit Advanced —如何构建2,5D游戏(第二部分)
- 【转】从头说catalan数及笔试面试里那些相关的问题
- jQuery1.3.2 源码学习 -1 $ 是什么?jQuery 又是什么?
- 小程序 php wecahtpay,【PHP】laravel 实现微信小程序微信支付功能
- 喜马拉雅三年亏损超20亿 腾讯、小米为股东
- spss分析qpcr数据_谁说菜鸟不会数据分析--SPSS篇
- linux下 mysql5.5数据库迁移操作
- 入门教程:.NET开源OpenID Connect 和OAuth解决方案IdentityServer v3 介绍 (一)
- ASCII码表、ASCII码扩展表
- 关于Java工具eclipse的基本
- php背景四周向中间渐变色,使用CSS巧妙地制作背景色渐变动画实例
- 云计算未来 — 云原生
- 戴尔发布XPS笔记本等新品;虎牙全面采用亚马逊云科技专用数据库服务 | 全球TMT...
- Java selenium 爬取 星图
- 使用Profiler进行性能分析
- 灌篮高手怎么找回原来的服务器,灌篮高手手游异常登陆、封号补偿及领取方式介绍...