public class T {
public static void main(String[] args) throws Exception {
Date d = new Date();
String abc ="2011-11-20 15:15:20";
SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date x =dt.parse(abc);
String y = dt.format(x);
Date xx =Calendar.getInstance().getTime();
GregorianCalendar gc= new GregorianCalendar();
gc.setTime(x);
gc.add(5, -2);
System.out.println(gc.getTime());
}
}
package com.unicom.common;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
public class CompareDate {
public String getNow2String(String format) {
Date now = new Date();
SimpleDateFormat fmt = new SimpleDateFormat(format);
return fmt.format(now);
}
public String getNow2String(String format) {
Date now = new Date();
SimpleDateFormat fmt = new SimpleDateFormat(format);
return fmt.format(now);
}
public Date getNow() {
Date now = new Date();
return now;
}
Date now = new Date();
return now;
}
public Date getNow2Date(String format) throws ParseException {
String dt = getNow2String(format);
DateFormat fmt = new SimpleDateFormat(format);
return fmt.parse(dt);
}
String dt = getNow2String(format);
DateFormat fmt = new SimpleDateFormat(format);
return fmt.parse(dt);
}
public Date string2Date(String dateStr, String format)
throws ParseException {
DateFormat fmt = new SimpleDateFormat(format);
return fmt.parse(dateStr);
}
public String date2String(Date orgindate, String format)
throws ParseException {
DateFormat fmt = new SimpleDateFormat(format);
return fmt.format(orgindate);
}
throws ParseException {
DateFormat fmt = new SimpleDateFormat(format);
return fmt.parse(dateStr);
}
public String date2String(Date orgindate, String format)
throws ParseException {
DateFormat fmt = new SimpleDateFormat(format);
return fmt.format(orgindate);
}
public Date dateChangeByDay(String orginDate, String format, int amount)
throws ParseException {
GregorianCalendar gc = new GregorianCalendar();
Date dt = string2Date(orginDate, format);
gc.setTime(dt);
gc.add(5, amount);
return gc.getTime();
throws ParseException {
GregorianCalendar gc = new GregorianCalendar();
Date dt = string2Date(orginDate, format);
gc.setTime(dt);
gc.add(5, amount);
return gc.getTime();
}
public Date dateChangeByMonth(String orginDate, String format, int amount)
throws ParseException {
GregorianCalendar gc = new GregorianCalendar();
Date dt = string2Date(orginDate, format);
gc.setTime(dt);
gc.add(2, amount);
return gc.getTime();
throws ParseException {
GregorianCalendar gc = new GregorianCalendar();
Date dt = string2Date(orginDate, format);
gc.setTime(dt);
gc.add(2, amount);
return gc.getTime();
}
public Date dateChangeByYear(String orginDate, String format, int amount)
throws ParseException {
GregorianCalendar gc = new GregorianCalendar();
Date dt = string2Date(orginDate, format);
gc.setTime(dt);
gc.add(1, amount);
return gc.getTime();
throws ParseException {
GregorianCalendar gc = new GregorianCalendar();
Date dt = string2Date(orginDate, format);
gc.setTime(dt);
gc.add(1, amount);
return gc.getTime();
}
public boolean greaterThan(Date date1, Date date2) {
return date1.before(date2);
}
return date1.before(date2);
}
public boolean lessThan(Date date1, Date date2) {
return date1.after(date2);
}
return date1.after(date2);
}
public boolean greaterThan(String date1, String date2, String format)
throws ParseException {
Date dateA = string2Date(date1, format);
Date dateB = string2Date(date2, format);
return dateA.before(dateB);
}
throws ParseException {
Date dateA = string2Date(date1, format);
Date dateB = string2Date(date2, format);
return dateA.before(dateB);
}
public boolean lessThan(String date1, String date2, String format)
throws ParseException {
Date dateA = string2Date(date1, format);
Date dateB = string2Date(date2, format);
return dateA.after(dateB);
}
}
throws ParseException {
Date dateA = string2Date(date1, format);
Date dateB = string2Date(date2, format);
return dateA.after(dateB);
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/21179375/viewspace-746117/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/21179375/viewspace-746117/