标签:io os ar java for sp 2014 c on
// 完整显示日期时间
String str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(new Date());
System.out.println(str);
// 创建 Calendar 对象
Calendar calendar = Calendar.getInstance();
// 初始化 Calendar 对象,但并不必要,除非需要重置时间
calendar.setTime(new Date());
// setTime 类似上面一行
// Date date = new Date();
// calendar.setTime(date);
// 显示年份
int year = calendar.get(Calendar.YEAR);
System.out.println("YEAR is = " + String.valueOf(year));
// 显示月份 (从0开始, 实际显示要加一)
int MONTH = calendar.get(Calendar.MONTH);
System.out.println("MONTH is = " + (MONTH + 1));
// 今年的第 N 天
int DAY_OF_YEAR = calendar.get(Calendar.DAY_OF_YEAR);
System.out.println("DAY_OF_YEAR is = " + DAY_OF_YEAR);
// 本月第 N 天
int DAY_OF_MONTH = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("DAY_OF_MONTH = " + String.valueOf(DAY_OF_MONTH));
// 3小时以后
calendar.add(Calendar.HOUR_OF_DAY, 3);
int HOUR_OF_DAY = calendar.get(Calendar.HOUR_OF_DAY);
System.out.println("HOUR_OF_DAY + 3 = " + HOUR_OF_DAY);
// 当前分钟数
int MINUTE = calendar.get(Calendar.MINUTE);
System.out.println("MINUTE = " + MINUTE);
// 15 分钟以后
calendar.add(Calendar.MINUTE, 15);
MINUTE = calendar.get(Calendar.MINUTE);
System.out.println("MINUTE + 15 = " + MINUTE);
// 30分钟前
calendar.add(Calendar.MINUTE, -30);
MINUTE = calendar.get(Calendar.MINUTE);
System.out.println("MINUTE - 30 = " + MINUTE);
// 格式化显示
str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());
System.out.println(str);
// 重置 Calendar 显示当前时间
calendar.setTime(new Date());
str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());
System.out.println(str);
// 创建一个 Calendar 用于比较时间
Calendar calendarNew = Calendar.getInstance();
// 设定为 5 小时以前,后者大,显示 -1
calendarNew.add(Calendar.HOUR, -5);
System.out.println("时间比较:" + calendarNew.compareTo(calendar));
// 设定7小时以后,前者大,显示 1
calendarNew.add(Calendar.HOUR, +7);
System.out.println("时间比较:" + calendarNew.compareTo(calendar));
// 退回 2 小时,时间相同,显示 0
calendarNew.add(Calendar.HOUR, -2);
System.out.println("时间比较:" + calendarNew.compareTo(calendar));
package com.minxinloan.common.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang.time.DateFormatUtils;
/**
* 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
* @author ThinkGem
* @version 2013-3-15
*/
public class DateUtils extends org.apache.commons.lang.time.DateUtils {
private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" };
/**
* 得到当前日期字符串 格式(yyyy-MM-dd)
*/
public static String getDate() {
return getDate("yyyy-MM-dd");
}
/**
* 得到当前日期字符串 格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
*/
public static String getDate(String pattern) {
return DateFormatUtils.format(new Date(), pattern);
}
/**
* 得到日期字符串 默认格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
*/
public static String formatDate(Date date, Object... pattern) {
String formatDate = null;
if (pattern != null && pattern.length > 0) {
formatDate = DateFormatUtils.format(date, pattern[0].toString());
} else {
formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
}
return formatDate;
}
/**
* 得到当前时间字符串 格式(HH:mm:ss)
*/
public static String getTime() {
return formatDate(new Date(), "HH:mm:ss");
}
/**
* 得到当前日期和时间字符串 格式(yyyy-MM-dd HH:mm:ss)
*/
public static String getDateTime() {
return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
}
/**
* 得到当前年份字符串 格式(yyyy)
*/
public static String getYear() {
return formatDate(new Date(), "yyyy");
}
/**
* 得到当前月份字符串 格式(MM)
*/
public static String getMonth() {
return formatDate(new Date(), "MM");
}
/**
* 得到当天字符串 格式(dd)
*/
public static String getDay() {
return formatDate(new Date(), "dd");
}
/**
* 得到当前星期字符串 格式(E)星期几
*/
public static String getWeek() {
return formatDate(new Date(), "E");
}
/**
* 日期型字符串转化为日期 格式
* { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
* "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" }
*/
public static Date parseDate(Object str) {
if (str == null){
return null;
}
try {
return parseDate(str.toString(), parsePatterns);
} catch (ParseException e) {
return null;
}
}
/**
* 获取过去的天数
* @param date
* @return
*/
public static long pastDays(Date date) {
long t = new Date().getTime()-date.getTime();
return t/(24*60*60*1000);
}
/**
* 计算两个日期之间相差的天数
*
* @param smdate
* 较小的时间
* @param bdate
* 较大的时间
* @return 相差天数
* @throws ParseException
*/
public static int daysBetween(Date smdate, Date bdate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
smdate = sdf.parse(sdf.format(smdate));
bdate = sdf.parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
// System.out.println(formatDate(parseDate("2010/3/6")));
// System.out.println(getDate("yyyy年MM月dd日 E"));
// long time = new Date().getTime()-parseDate("2012-11-19").getTime();
// System.out.println(time/(24*60*60*1000));
// String now = "2014-06-11";
// String now1 = "2014-06-11 16:32:00";
//
// System.out.println(new Date());
// System.out.println(new Date("2014-06-11 16:32:00").getTime());
// System.out.println(new Date("2014-06-11").getTime());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
Date lendDay = sdf1.parse("2013-11-19");
Calendar cal = Calendar.getInstance();
Date nowDay = sdf.parse(sdf.format(cal.getTime()));
System.out.println(cal.getTime());
cal.setTime(lendDay);
System.out.println(cal.getTime());
long time1 = cal.getTimeInMillis();
System.out.println(time1);
}
}
标签:io os ar java for sp 2014 c on
原文地址:http://www.cnblogs.com/yy123/p/4011042.html