java Date获取年月日时分秒的实现方法

所属分类: 软件编程 / java 阅读数: 51
收藏 0 赞 0 分享

java 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); 
 } 
} 

以上这篇java Date获取年月日时分秒的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多