package pack.java.calendar.demo \x0d\x0a \x0d\x0aimport java.text.SimpleDateFormat \x0d\x0aimport java.util.Calendar \x0d\x0aimport java.util.Date \x0d\x0aimport java.util.GregorianCalendar \x0d\x0a \x0d\x0apublic class CalendarDemo { \x0d\x0aprivate static SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss") \x0d\x0apublic static void main(String[] args) { \x0d\x0a \x0d\x0a//获取calendar实例 \x0d\x0aCalendar calendar = Calendar.getInstance() \x0d\x0a \x0d\x0a//判断calendar是不是GregorianCalendar类的实例 \x0d\x0aif(calendar instanceof GregorianCalendar){ \x0d\x0aSystem.out.println("属于GregorianCalendar类的实例!") \x0d\x0a} \x0d\x0a \x0d\x0a//从calendar对象中获得date对象,当前
时间 \x0d\x0aDate dates = calendar.getTime() \x0d\x0a \x0d\x0a//格式化时间 \x0d\x0aString date_str= date_format.format(dates) \x0d\x0aSystem.out.println(date_str) \x0d\x0a \x0d\x0a//设置月份05;代表日历的月份6月,因为月份从0开始。 \x0d\x0acalendar.set(Calendar.MONTH, 05) \x0d\x0a \x0d\x0aint months = calendar.get(Calendar.MONTH) \x0d\x0aSystem.out.println(months) //输出05 \x0d\x0a \x0d\x0a//设置
日期为2011-07-24 09:59:50 \x0d\x0acalendar.set(2011, 06, 24, 9, 59, 50)\x0d\x0aString getDate = date_format.format(calendar.getTime()) \x0d\x0aSystem.out.println(getDate) //输出2011-07-24 09:59:50 \x0d\x0a \x0d\x0a//比较日前大小 \x0d\x0aif(new Date().getTime() >calendar.getTimeInMillis()){ \x0d\x0aSystem.out.println("当前日期在后!") \x0d\x0a}else{ \x0d\x0aSystem.out.println("当前日期在前!") \x0d\x0a} \x0d\x0a \x0d\x0a//设置当前时间为:2011-07-24 11:06:00 \x0d\x0acalendar.setTime(new Date())\x0d\x0aint year = calendar.get(Calendar.YEAR) //获取年 \x0d\x0aint month = calendar.get(Calendar.MONTH) //获取月 \x0d\x0aint date = calendar.get(Calendar.DATE) //获取天 \x0d\x0aint hour = calendar.get(Calendar.HOUR) //获取小时 \x0d\x0aint minute = calendar.get(Calendar.MINUTE)//获取分钟 \x0d\x0aint second = calendar.get(Calendar.SECOND)//获取秒钟 \x0d\x0aint hour_of_day = calendar.get(Calendar.HOUR_OF_DAY) //第几个小时, \x0d\x0aint day_of_month = calendar.get(Calendar.DAY_OF_MONTH)//这天,在一个月内是第
几天.\x0d\x0aint day_of_week = calendar.get(Calendar.DAY_OF_WEEK) //这天,在一周内,是第几天. \x0d\x0aint day_of_year = calendar.get(Calendar.DAY_OF_YEAR) //这天,在一年内,是第几天。 \x0d\x0aint week_of_year = calendar.get(Calendar.WEEK_OF_YEAR) //这周,在一年内是第几周 \x0d\x0aint week_of_month = calendar.get(Calendar.WEEK_OF_MONTH)//这周,在这个月是第几周以以星为标准; \x0d\x0aint zone_offset = calendar.get(Calendar.ZONE_OFFSET) //获取时区 \x0d\x0aint day_of_week_in_month = calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH) //某月中第几周,按这个月1号算,1号起就是第1周,8号起就是第2周。以月份天数为标准\x0d\x0aint r = calendar.get(Calendar.AM_PM) \x0d\x0aif(r==calendar.AM){ \x0d\x0aSystem.out.println("现在是上午") \x0d\x0a} \x0d\x0a \x0d\x0aif(r==calendar.PM){ \x0d\x0aSystem.out.println("现在是下午") \x0d\x0a} \x0d\x0aSystem.out.println("==================================================") \x0d\x0aSystem.out.println(year) \x0d\x0aSystem.out.println(month) \x0d\x0aSystem.out.println(date) \x0d\x0aSystem.out.println(hour) \x0d\x0aSystem.out.println(minute) \x0d\x0aSystem.out.println(second) \x0d\x0aSystem.out.println(hour_of_day) \x0d\x0aSystem.out.println(day_of_month) \x0d\x0aSystem.out.println(day_of_week) \x0d\x0aSystem.out.println(day_of_year) \x0d\x0aSystem.out.println(week_of_year) \x0d\x0aSystem.out.println(week_of_month) \x0d\x0aSystem.out.println(zone_offset) \x0d\x0aSystem.out.println(day_of_week_in_month) \x0d\x0a} \x0d\x0a}JAVA中获取当前系统时间2011-07-06 20:45 并格式化输出:
import java.util.Date
import java.text.SimpleDateFormat
public class NowString {
public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")//设置日期格式
System.out.println(df.format(new Date()))// new Date()为获取当前系统时间
}
}
设置时间,推荐 使用java.util.Calendar类来进行 *** 作,
import java.util.Date
import java.util.Calendar
import java.text.SimpleDateFormat
public class TestDate{
public static void main(String[] args){
Date now = new Date()
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")//可以方便地修改日期格式
String hehe = dateFormat.format( now )
System.out.println(hehe)
Calendar c = Calendar.getInstance()//可以对每个时间域单独修改
int year = c.get(Calendar.YEAR)
int month = c.get(Calendar.MONTH)
int date = c.get(Calendar.DATE)
int hour = c.get(Calendar.HOUR_OF_DAY)
int minute = c.get(Calendar.MINUTE)
int second = c.get(Calendar.SECOND)
System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second)
}
}
评论列表(0条)