代码:
package test
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
public class test {
public static void main(String[] args)
{
//使用默认时区和语言环境获得一个日历
Calendar cale = Calendar.getInstance()
System.out.println("____________________________________")
System.out.println("打印calendar对象:")
System.out.println(cale)
//将Calendar类型转换成Date类型
Date tasktime=cale.getTime()
//设置日期输出的格式
System.out.println("将Calendar类型转换成Date类型,格式化输出后打印:")
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
//格式化输出
String nowTime = df.format(tasktime)
System.out.println(nowTime)
int year = cale.YEAR
int month = cale.MONTH+1
int day = cale.DAY_OF_MONTH
System.out.println("cale.YEAR,cale.MONTH,cale.DAY_OF_MONTH 分别为:"+year+","+month+","+day+",")
System.out.println("____________________________________")
System.out.println("calendar.get(常量)test:")
int y = cale.get(cale.YEAR)
int m = cale.get(cale.MONTH)+1
int d = cale.get(cale.DAY_OF_MONTH)
int week = cale.get(cale.WEEK_OF_MONTH)-1
//输出当前日期
System.out.println(y+"年"+m+"月"+d+"日 "+"星期"+week)
System.out.println("____________________________________")
System.out.println("getTime()方法test:")
Calendar c2 = Calendar.getInstance()
Date date = c2.getTime()
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd")
long time = date.getTime()
String nowDate = sdf.format(date)
System.out.println("当前时间:"+nowDate)
System.out.println(date+"\n当前毫秒数:"+time)
Calendar c3 = Calendar.getInstance()
c3.set(2011, 11, 11)
Date date3 = c3.getTime()
long time3 = date3.getTime()
System.out.println(date3+"的系统时间毫秒为:\n"+time3)
long x = time-time3
long dd=x/(1000*60*60*24)
System.out.println("2011距离当前的毫秒数:"+x+"\n距离当前的天数为:"+dd+"天")
System.out.println("____________________________________")
System.out.println("Date+get()方法输出时间(已过时):")
Date DateTime1=new Date()
int date_year=DateTime1.getYear()+1900
int date_month=DateTime1.getMonth()+1
int date_day=DateTime1.getDate()
int hours=DateTime1.getHours()
int minutes=DateTime1.getMinutes()
int seconds=DateTime1.getSeconds()
String date_time=date_year+"-"+date_month+"-"+date_day+" "+hours+":"+minutes+":"+seconds
System.out.println(date_time)
System.out.println("____________________________________")
System.out.println("Date+sdf输出时间:")
Date DateTime2=new Date()
SimpleDateFormat date_sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
String date_time2=date_sdf.format(DateTime2)
System.out.println(date_time2)
}
}
输出为:
____________________________________
打印calendar对象:
java.util.GregorianCalendar[time=1520571549226,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=2,WEEK_OF_YEAR=10,WEEK_OF_MONTH=2,DAY_OF_MONTH=9,DAY_OF_YEAR=68,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=0,HOUR_OF_DAY=12,MINUTE=59,SECOND=9,MILLISECOND=226,ZONE_OFFSET=28800000,DST_OFFSET=0]
将Calendar类型转换成Date类型,格式化输出后打印:
2018-03-09 12:59:09
cale.YEAR,cale.MONTH,cale.DAY_OF_MONTH 分别为:1,3,5,
____________________________________
calendar.get(常量)test:
2018年3月9日 星期1
____________________________________
getTime()方法test:
当前时间:2018-03-09
Fri Mar 09 12:59:09 CST 2018
当前毫秒数:1520571549267
Sun Dec 11 12:59:09 CST 2011的系统时间毫秒为:
1323579549272
2011距离当前的毫秒数:196991999995
距离当前的天数为:2279天
____________________________________
Date+get()方法输出时间(已过时):
2018-3-9 12:59:9
____________________________________
Date+sdf输出时间:
2018-03-09 12:59:09