Java日期獲取需求大全

剛進公司,做爲熟悉技術,爲公司作了一個小的點餐系統的網站,其中大量用到了時間日期做爲惟一標示或是顯示設置。特總結了一下和你們分享。java

package com.lucis.ordering.Utils;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;/** * Created by JayZhao on 2015/8/17 0017. *  系統日期獲取控件 */public class DateUtil {    //實例化date對象    Date date = new Date();    //格式化成年月日式 date 類型    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");    //格式化年月日 時分秒  date 類型    SimpleDateFormat detailsDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    //解析成年-月-日 格式date形數據    public Date SimpleSystemTime(){        String now =new SimpleDateFormat("yyyy-MM-dd").format(date);        try {            return simpleDateFormat.parse(now);        } catch (ParseException e) {            e.printStackTrace();        }        return date;    }    //解析成年-月-日 格式String形數據    public String SimpleSystemTimeString(){        return new SimpleDateFormat("yyyy-MM-dd").format(date);    }    //解析成年-月-日 時:分:秒 格式date形數據    public Date DetailSystemTime(){        String now =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);        try {            return detailsDateFormat.parse(now);        } catch (ParseException e) {            e.printStackTrace();        }        return date;    }    //解析成年-月-日 星期幾 格式date形數據    public  String  MiddleSystemTime() {        String now =new SimpleDateFormat("yyyy-MM-dd").format(date);        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};        Calendar cal = Calendar.getInstance();        cal.setTime(date);        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;        if (w < 0) {            w = 0;        }        return now+" "+weekDays[w];    }    //根據時間獲取當日星期    public int WeekDays(String day){        Calendar cal = Calendar.getInstance();        try {            cal.setTime(simpleDateFormat.parse(day));        } catch (ParseException e) {            e.printStackTrace();        }        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;        return w;    }    //解析當前日期前幾天成 年-月-日 格式的date數據    public String Yesterday(){        Calendar calendar = Calendar.getInstance();//此時打印它獲取的是系統當前時間        calendar.add(Calendar.DATE, -1);    //獲得前一天        return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());    }    //解析某一天的前幾天    public String getSpecifiedDayBefore(String specifiedDay,int d){        Calendar c = Calendar.getInstance();        Date date=null;        try {            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);        } catch (ParseException e) {            e.printStackTrace();        }        c.setTime(date);        int day=c.get(Calendar.DATE);        c.set(Calendar.DATE,day-d);        String dayBefore=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());        return dayBefore;    }    //比較兩個時間大小    public boolean compare(String date1,String date2){        boolean result = false;        try {             result = simpleDateFormat.parse(date1).before(simpleDateFormat.parse(date2));        } catch (ParseException e) {            e.printStackTrace();        }        return result;    }    //根據一天獲取一週時間    public String testDate(String newtime) {        String dayNames[] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};        int week1[] = {7,1,2,3,4,5,6};        Calendar c = Calendar.getInstance();// 得到一個日曆的實例        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");        try {            c.setTime(sdf.parse(newtime));        } catch (ParseException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        String a=dayNames[c.get(Calendar.DAY_OF_WEEK)-1];        int b=week1[c.get(Calendar.DAY_OF_WEEK)-1];        if(b!=7){            c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);        }else{            c.add(Calendar.WEEK_OF_MONTH, -1);            c.set(Calendar.DAY_OF_WEEK, 2);        }        return a;    }    //獲取某一天的下週一    public String getNextMonday(Date date){        Calendar cal = Calendar.getInstance();        cal.setTime(date);        int week = cal.get(Calendar.DAY_OF_WEEK);        if(week>2){            cal.add(Calendar.DAY_OF_MONTH,-(week-2)+7);        }else{            cal.add(Calendar.DAY_OF_MONTH,2-week+7);        }        return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());    }    //獲取某一天的本週一    public String getMonday(Date date){        Calendar cal = Calendar.getInstance();        cal.setTime(date);        int week = cal.get(Calendar.DAY_OF_WEEK);        if(week>2){            cal.add(Calendar.DAY_OF_MONTH,-(week-2));        }else{            cal.add(Calendar.DAY_OF_MONTH,2-week);        }        return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());    }}
相關文章
相關標籤/搜索