/**
* explain : 简单的对时间的格式化及操作时间进行总结
* author : 杜明星
* date : 20120321
*/
package com.test;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class test {
/**
* 直接输入未经任何操作的时间
*
* @param args
*/
public String getTime() {
Date date = new Date();
String currenttime = String.valueOf(date.getTime());
System.out.println("直接输入未经任何操作的时间格式--->" + currenttime);
return currenttime;
}
/**
* 利用SimpleDateFormat进行格式化
*
* @param args
*/
public String getSimpleDateFormat() {
Date date = new Date();
// 格式化后的格式为:星期三-三月-21-2012
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("EEEE-MMMM-dd-yyyy");
// 格式化后的格式为:2012-03-21 13:19:29 星期三
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
// 格式化后的格式为:2012年03月21日 13时21分27秒星期三
SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E");
// 格式化后的格式为:20120321 132330
SimpleDateFormat simpleDateFormat4 = new SimpleDateFormat("yyyyMMdd HHmmss");
String currenttime1 = simpleDateFormat1.format(date);
String currenttime2 = simpleDateFormat2.format(date);
String currenttime3 = simpleDateFormat3.format(date);
String currenttime4 = simpleDateFormat4.format(date);
System.out.println("经过SimpleDateFormat格式化后的时间--->" + currenttime1);
System.out.println("经过SimpleDateFormat格式化后的时间--->" + currenttime2);
System.out.println("经过SimpleDateFormat格式化后的时间--->" + currenttime3);
System.out.println("经过SimpleDateFormat格式化后的时间--->" + currenttime4);
return currenttime1;
}
/**
* 通过JAVA类库提供的标准格式输出时间
*
* @param args
*/
public String getDateFormat() {
Date date = new Date();
// 格式化后的格式为:12-3-21 下午1:32
DateFormat dateFormat1 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
String currenttime = dateFormat1.format(date);
System.out.println("经过DateFormat格式化后的时间--->" + currenttime);
return currenttime;
}
/**
* 利用Calendar进行对时间的操作
*
* @param args
*/
public String getCalendar() {
//格式化后的时间格式为:2012-03-21 13:43:08 星期三
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
System.out.println("今天--->" + dateformat.format(new Date()));
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
Date d1 = new Date(c.getTimeInMillis());
System.out.println("星期一--->" + dateformat.format(d1)); //格式化后的时间格式为:2012-03-19 13:43:08 星期一
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
Date d2 = new Date(c.getTimeInMillis());
System.out.println("星期日--->" + dateformat.format(d2)); //格式化后的时间格式为:2012-03-18 13:43:08 星期日
String currenttime = dateformat.format(d1);
return currenttime;
}
/**
* 获取当前时间的前N天(利用java.util.Calender来实现)
* @param args
*/
public static String getCalenderBeforeDate(Date date,int days){
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR) - days);
String beforedate = df.format(calendar.getTime());
System.out.println("当前系统时间N天前的时间--->" + beforedate);
return beforedate;
}
/**
* 获取当前时间的后N天时间(利用java.util.Calender来实现)
* @param args
*/
public static String getCalenderAfterDate(Date date,int days){
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR) + days);
String afterdate = df.format(calendar.getTime());
System.out.println("当前系统时间N天后的时间--->" + afterdate);
return df.format(calendar.getTime());
}
/**
* 获取当前时间N天前的时间(用java.text.SimpleDateFormat和java.util.Date来实现)
* @param args
*/
public static String getBeforeDate(Date date,int days){
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
Date beforeDate = new Date(date.getTime() - days * 24 * 60 * 60 * 1000);
String beforedate = df.format(beforeDate);
System.out.println("当前系统时间N天后的时间--->" + beforedate);
return beforedate;
}
/**
* 获取当前时间的后N天时间(用java.text.SimpleDateFormat和java.util.Date来实现)
* @param args
*/
public static String getAfterDate(Date date,int days){
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
Date afdate = new Date(date.getTime() + days * 24 * 60 * 60 * 1000);
String afterdate = df.format(afdate);
System.out.println("当前系统时间N天后的时间--->" + afterdate);
return afterdate;
}
public static void main(String[] args) {
test t = new test();
t.getTime();
t.getSimpleDateFormat();
t.getDateFormat();
t.getCalendar();
t.getCalenderBeforeDate(new Date(),5);
t.getCalenderAfterDate(new Date(),5);
t.getBeforeDate(new Date(),3);
t.getAfterDate(new Date(), 3);
}
}