Home > Java > javaTutorial > Detailed explanation of Java date conversion and date time conversion

Detailed explanation of Java date conversion and date time conversion

黄舟
Release: 2017-10-11 09:52:04
Original
2486 people have browsed it

This article mainly introduces Java date and time in detail, as well as the method of converting dates to each other. It has certain reference value. Interested friends can refer to

Java date and time, and mutual conversion methods. Conversion, for your reference, the specific content is as follows


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
 }

}
Copy after login

The above is the detailed content of Detailed explanation of Java date conversion and date time conversion. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template