package com.baizhan; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class TestDate { public static void main(String[] args) throws ParseException { //testDate(); //formateDate(); parseDate("2011/2/14 13:14:15"); } static void testDate(){ long curtime = System.currentTimeMillis(); //new Date()其本身就返回当前时间ms,返回为Date对象 Date d = new Date(); //也可直接传入对应的ms数 Date d1 = new Date(1000L*3600*24*30*150); System.out.println(d1.getTime()); } static void formateDate(){ DateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"); String df_curr = df.format(new Date()); System.out.println(df_curr); } static void parseDate(String date) throws ParseException { DateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"); Date pd = df.parse(date); long rtime = pd.getTime(); System.out.println(rtime); System.out.println(df.format(rtime)); } static void calendarDate(){ Calendar calendar = new GregorianCalendar(2023,8,12,14,15,16); //获取年 int year = calendar.get(Calendar.YEAR); //设置年 calendar.set(Calendar.YEAR,2023); //返回时间对象 Date d = calendar.getTime(); //返回当前时间(ms) Long t = calendar.getTimeInMillis(); //日期计算 calendar.add(Calendar.DATE,1000); //向后推1000天 calendar.add(Calendar.YEAR,30); //向前推30年 } }
标签:df,import,时间,应用,new,Date,相关,calendar,Calendar From: https://www.cnblogs.com/xjklmycw/p/17175473.html