Date日期操作-年月日,时分秒获取
直接上代码
package com.util; import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date; public class Test { public void getTimeByDate(){ Date date = new Date(); DateFormat df1 = DateFormat.getDateInstance();//日期格式,精确到日 System.out.println(df1.format(date)); DateFormat df2 = DateFormat.getDateTimeInstance();//可以精确到时分秒 System.out.println(df2.format(date)); DateFormat df3 = DateFormat.getTimeInstance();//只显示出时分秒 System.out.println(df3.format(date)); DateFormat df4 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //显示日期,周,上下午,时间(精确到秒) System.out.println(df4.format(date)); DateFormat df5 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //显示日期,上下午,时间(精确到秒) System.out.println(df5.format(date)); DateFormat df6 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //显示日期,上下午,时间(精确到分) System.out.println(df6.format(date)); DateFormat df7 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //显示日期,时间(精确到分) System.out.println(df7.format(date)); } public void getTimeByCalendar(){ Calendar cal = Calendar.getInstance(); int year = cal.get(Calendar.YEAR);//获取年份 int month=cal.get(Calendar.MONTH);//获取月份 int day=cal.get(Calendar.DATE);//获取日 int hour=cal.get(Calendar.HOUR);//小时 int minute=cal.get(Calendar.MINUTE);//分 int second=cal.get(Calendar.SECOND);//秒 int WeekOfYear = cal.get(Calendar.DAY_OF_WEEK);//一周的第几天 System.out.println("现在的时间是:公元"+year+"年"+month+"月"+day+"日 "+hour+"时"+minute+"分"+second+"秒 星期"+WeekOfYear); } public static void main(String[] args) { Test t=new Test(); t.getTimeByDate(); System.out.println("****************************"); t.getTimeByCalendar(); }
}
获取日,如果大于16则+2个月,否则+1个月,输出7个月
public static void main(String[] as) throws Exception { int j; Calendar cc = Calendar.getInstance(); int myYear = cc.get(Calendar.YEAR); int myMonth = cc.get(Calendar.MONTH); j=cc.get(Calendar.DATE)>16?3:2; cc.set(Calendar.MONTH, myMonth+j); System.out.println(myYear+"-"+cc.get(cc.MONTH)); for(int i=0;i<7;i++){ cc.add(cc.MONTH, 1); int mm = cc.get(cc.MONTH); int mmm=mm==0?12:mm; System.out.println(myYear+"-"+mmm); }
}
原文链接:Date日期操作-年月日,时分秒获取 - zhengbeibei - 博客园
Date日期操作-年月日,时分秒获取相关推荐
- 获取今天的日期(年月日时分秒)、获取今天昨天明天的日期
1.获取今天的日期(年月日时分秒) getCurrentTime() {let yy = new Date().getFullYear();let mm = new Date().getMonth() ...
- java时间格式年月日时分秒_java日期格式(年月日时分秒毫秒)
java日期格式(年月日时分秒毫秒) 2020-08-13 07:09 阅读数 79 package test.remote.tools.combine; import java.text.Simpl ...
- php日期格式化年月日时分秒,php Date()函数输出中文年月日时分秒
php Date()函数输出中文年月日时分秒今天我以一个简单的利用date()函数输出中文年月日时分秒引出关于大多数据用户在使用php date()函数的一些问题与date()函数一些参数详解. 当然 ...
- js 日期函数 将 js new Date() 转化为年月日时分秒
let date = new Date(); let year = date.getFullYear(); //年let month = date.getMonth() + 1; //月let str ...
- Python画数码晶体管日期(年月日时分秒)
源码: drawLine(draw) 画一条线 drawDight(dight) 画一个数字 drawDate(date) 画一组数字 import turtle, timedef drawLine( ...
- php日期格式化年月日时分秒,js将当前时间格式化为年月日时分秒格式详解
本文主要介绍了js将当前时间格式化为 年-月-日 时:分:秒主要是使用js的Date()对象,将系统当前时间格式化为年-月-日 时:分:秒,需要的朋友可以参考下,希望能帮助到大家. 利用js的Date ...
- JavaScript中的内置对象-8--4.date对象中-获取,设置日期时间的方法; 获取,设置年月日时分秒及星期的方法;...
学习目标 1.掌握创建日期对象的方法 2.掌握date对象中获取日期时间的方法 3.掌握date对象中设置日期时间的方法 如何创建一个日期对象 语法:new Date(); 功能:创建一个日期时间对象 ...
- java Date获取 年月日时分秒
转载自 java Date获取 年月日时分秒 package com.util;import java.text.DateFormat; import java.util.Calendar; im ...
- android获取年月日时分秒毫秒,Android获取两个日期其间间隔的天数
/** * 获取两个日期之间的间隔天数 * @return */ public static int getGapCount(Date startDate, Date endDate) { Calen ...
最新文章
- php-fpm慢执行日志
- python【数据结构与算法】最短路算法之FloyedDijkstra
- [深度学习] 自然语言处理 --- BERT模型原理
- aws集群重启_在AWS中设置Cassandra集群
- linux 目录结构+常用命令+压缩命令+vim使用+及一些基础知识(非常好)
- Android 滑动菜单框架--SwipeMenuListView框架完全解析
- OpenCV-绘制简易直方图DrawHistImg
- userService 用户 会员 系统设计 v2 q224 .doc
- 缓存击穿 穿透 雪崩_缓存雪崩、缓存穿透、缓存击穿的问题
- 达观数据郭权:用好ngResource和postman,提高你的开发调试效率
- Segger Embedded Studio使用有什么技巧?
- Hexo+GithubPage搭建Fan主题的博客(7)配置自定义域名
- SQL Server数据库的管理及维护
- ApacheCN 计算机视觉译文集 20210218 更新
- JS字符串转Byte[]
- 如何将wps中的表格转为图片,并设置较高的dpi
- 面试总结:任意一个整数分解为几个连续正整数之和
- 隐藏input文本框的边框
- linux 系统硬件信息检测工具,美国Linux服务器查看硬件信息工具
- 安装SSL证书遇到困难怎么办?