Java常用类:时间日期类

一: java.util.Date类

构造器:

Java常用类:时间日期类_第1张图片

示例:

	public static void main(String[] args) {

		Date d1 = new Date();
		Date d2 = new Date(System.currentTimeMillis()+10000);
		
		System.out.println("d1:"+d1+";d2:"+d2); 
		System.out.println(d2.before(d1));//是否在其之前
		System.out.println(d1.compareTo(d2));//
	}
输出:     d1:Tue Mar 22 13:47:47 CST 2016;d2:Tue Mar 22 13:47:57 CST 2016
             false
             -1

示例:

	public static String generateTimeCode() {
		Long time  = new Date().getTime()/1000L;
		return Long.toHexString(time);
	}


二: java.util.Calendar类




示例:

	public static void main(String[] args) {

		Date d1 = new Date();
		System.out.println("d1:" + d1);

		Calendar calendar = Calendar.getInstance();
		Date date = calendar.getTime();
		Calendar calendar2 = Calendar.getInstance();
		calendar2.setTime(date);
		System.out.println("date:" + date);
		System.out.println("calendar:" + calendar);
		System.out.println("calendar2:" + calendar2);
	}

输出:
d1:Tue Mar 22 14:01:31 CST 2016
date:Tue Mar 22 14:01:31 CST 2016
calendar:java.util.GregorianCalendar[time=1458626491449,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=2016,MONTH=2,WEEK_OF_YEAR=13,WEEK_OF_MONTH=4,DAY_OF_MONTH=22,DAY_OF_YEAR=82,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=2,HOUR_OF_DAY=14,MINUTE=1,SECOND=31,MILLISECOND=449,ZONE_OFFSET=28800000,DST_OFFSET=0]
calendar2:java.util.GregorianCalendar[time=1458626491449,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=2016,MONTH=2,WEEK_OF_YEAR=13,WEEK_OF_MONTH=4,DAY_OF_MONTH=22,DAY_OF_YEAR=82,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=2,HOUR_OF_DAY=14,MINUTE=1,SECOND=31,MILLISECOND=449,ZONE_OFFSET=28800000,DST_OFFSET=0]

Date和Calendar都是表示日期的工具类,他们之间可以自由转换

示例:

	public static void main(String[] args) {

		Calendar calendar = Calendar.getInstance();

		System.out.println(calendar.get(YEAR));
		System.out.println(calendar.get(MONTH));
		System.out.println(calendar.get(DATE));
		
		calendar.set(2016, 04, 04);
		System.out.println(calendar.getTime());
	}
输出:
2016
2
22
Wed May 04 14:28:25 CST 2016


月的范围:0~11

示例:

	/**
	 * 功能描述:获取当前日期时间格式化字符串
	 * 
	 * @param dateFormat
	 *            日期时间格式
	 */
	public static String getCurrentDateTime(String dateFormat) {
		return new SimpleDateFormat(dateFormat).format(Calendar.getInstance()
				.getTime());
	}

	public static String getOffsetDate(String startTime, int field, int offset) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(sdf.parse(startTime));
			calendar.roll(field, offset);
			String offsetTime = sdf.format(calendar.getTime());
			return offsetTime;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}



三: java.text.SimpleDateFormat类


Date and Time Patterns:

Java常用类:时间日期类_第2张图片

构造器:




示例1:



//IConstant.java
public interface IConstant {
	public final static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public final static String SHORT_DATE_FORMAT = "yyyy-MM-dd";
}
SimpleDateFormat sdf = new SimpleDateFormat(IConstant.DEFAULT_DATE_FORMAT);
registerAddPO.setStartTime(sdf.format(new Date()));

其中,

Java常用类:时间日期类_第3张图片
Java常用类:时间日期类_第4张图片

示例2:

	public static void main(String[] args) {
		SimpleDateFormat sdf = new SimpleDateFormat(IConstant.DEFAULT_DATE_FORMAT);
		System.out.println("The Time is :\n"+sdf.format(new Date()));
	}

示例3:

	public static void main(String[] args) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G");
		System.out.println(sdf.format(new Date()));
		
		String str = "2016###三月###21";
		SimpleDateFormat sdf1 = new SimpleDateFormat("y###MMM###d");
		System.out.println(sdf1.parse(str));
		
	}
//输出  2016.03.22 公元
            Mon Mar 21 00:00:00 CST 2016

Java常用类:时间日期类_第5张图片

四: java.text.DateFormat类


public abstract class DateFormat extends Format
 
  
 
  

五:java.text.NumberFormat类

public abstract class NumberFormatextends Format

Java常用类:时间日期类_第6张图片


你可能感兴趣的:(Java常用类:时间日期类)