当前位置: 移动技术网 > IT编程>开发语言>Java > Java日期时间以及日期相互转换

Java日期时间以及日期相互转换

2019年07月19日  | 移动技术网IT编程  | 我要评论

java日期时间,以及相互转化,供大家参考,具体内容如下

package com.study.string;

import java.text.parseexception;
import java.text.simpledateformat;
import java.util.calendar;
import java.util.date;
import java.util.gregoriancalendar;

public class datebase {

 public static void main(string[] args) throws parseexception {
  /*
   * 获得当前时间
   */
  date date1 = new date();
  long long1 = date1.gettime();//date类型,转为 long类型
  system.out.println(date1);//sat aug 26 08:36:36 gmt+08:00 2017
  system.out.println(long1);//1503708031359
  
  calendar cale1 = calendar.getinstance();
  date1 = cale1.gettime();//calendar 类型 转为 date类型
  long1 = date1.gettime();
  system.out.println(cale1);
  system.out.println(date1);//sat aug 26 08:36:36 gmt+08:00 2017
  system.out.println(long1);
  
  /*
   *设置时间 
   */
  long1 += 24*60*60*1000;
  date1.settime(long1);
  system.out.println(date1);//sun aug 27 08:43:26 gmt+08:00 2017
  
  /*
   * 格式化时间日期,无参数的默认格式,有参数的自定义格式。
   * date -> string 用 format()
   * string -> date 用 parse()
   */
  simpledateformat sim1 = new simpledateformat();//默认格式:17-8-27 上午8:45
  string time1 = sim1.format(date1);
  system.out.println(time1);//17-8-27 上午8:45 
  date date11 = sim1.parse(time1);
  system.out.println(date11);
  
  simpledateformat sim2 = new simpledateformat("yyyy-mm-dd hh:mm:ss sss");
  string time2 = sim2.format(date1);
  system.out.println(time2);//2017-08-27 08:47:58 058
  date date22= sim2.parse(time2);
  system.out.println(date22);//sun aug 27 08:52:08 gmt+08:00 2017
  
  /*
   * 创建指定日期 string 
   * gregoriancalendar
   */
  simpledateformat sim3 = new simpledateformat("yyyy-mm-dd");
  string str1 = "2014-09-27";
  date date33 = sim3.parse(str1);
  system.out.println(date33);//sat sep 27 00:00:00 gmt+08:00 2014
  
  
  gregoriancalendar gre1 = new gregoriancalendar(2015,calendar.december,25);
  date date44 = gre1.gettime();
  system.out.println(date44);//fri dec 25 00:00:00 gmt+08:00 2015
  
  
  calendar cal2 = calendar.getinstance();
  cal2.set(calendar.year, 2017);
  cal2.set(calendar.month, 7);//月份的数字与 第几个月差1, 8 == calendar.september
  cal2.set(calendar.date, 26);// tue sep 09 09:04:25 gmt+08:00 2008
//  cal2.set(calendar.day_of_month, 12);
  system.out.println(cal2.gettime());//sat aug 26 09:09:44 gmt+08:00 2017
  
  
  
  /*
   * 获取年月日,星期,时间
   */
  int dayofweek = cal2.get(calendar.day_of_week);
  system.out.println(dayofweek);//7 是星期六
  
  
  /*
   * calendar 的时间加减
   */
  calendar cal3 = calendar.getinstance();
  cal3.add(calendar.year, 1);
  cal3.add(calendar.month, -2);
  system.out.println(cal3.gettime());//tue jun 26 09:14:11 gmt+08:00 2018
  
  
  /*
   * 
   */
  calendar cal4 = calendar.getinstance();
  cal4.set(calendar.year, 2016);
  cal4.set(calendar.date, 1);
  //每个月的最后 一天
  for(int month = calendar.january;month<calendar.december; month++){
    cal4.set(calendar.month, month);
    system.out.println(cal4.get(calendar.year)+"年"+(month+1)+"月"+
      cal4.getactualmaximum(calendar.date)+"日");
  }

/*
2016年1月31日
2016年2月29日
2016年3月31日
2016年4月30日
2016年5月31日
2016年6月30日
2016年7月31日
2016年8月31日
2016年9月30日
2016年10月31日
2016年11月30日
   */
  
  //直接创建long 型的时间
  long long2 = system.currenttimemillis();
  date daten = new date(long2);
  system.out.println(daten);//sat aug 26 09:41:08 gmt+08:00 2017
 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网