- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- public class DateTest {
- /**
- * 格式:yyyy-MM-dd
- */
- public String outputDate(String str) {
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
- String result = null;
- try {
- SimpleDateFormat ff = new SimpleDateFormat("yyyy-MM-dd");
- Calendar cal = Calendar.getInstance();
- cal.setTime(formatter.parse(str));
- cal.add(Calendar.DAY_OF_MONTH, -1);
- result = ff.format(cal.getTime());
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return result;
- }
- public String outputDate(Date date) {
- //日期格式
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.add(Calendar.DAY_OF_MONTH, -1);
- //返回
- return formatter.format(cal.getTime());
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- DateTest dt = new DateTest();
- System.out.println("string-RESULT=" + dt.outputDate("2008-11-01"));
- System.out.println("date-RESULT="+dt.outputDate(new Date()));
- }
- }
- 原创 salin
关于日期输出的东西
最新推荐文章于 2019-09-25 10:09:59 发布