java日期時間各種變換及處理

HFUT_qianyang發表於2018-05-10

本文作者:合肥工業大學 管理學院 錢洋 email:1563178220@qq.com 內容可能有不到之處,歡迎交流。
未經本人,允許禁止轉載。

java處理時間

個人在平時寫程式的時候,會遇到將字元型轉化為時間、或者獲取當期時間,昨天的時間,獲取當前月,當前日,當前季度等等。為此,本人寫了一個時間處理類,希望對大家有幫助。其中,該類中的函式,可能還會在以後遇到問題時,繼續擴充。

程式

package util;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
 * @author:合肥工業大學 管理學院 錢洋
 * @email:1563178220@qq.com
 * @ 
 */
public class TimeUtils {

    public static void main( String[] args ) throws ParseException{
        List<String> monthlist = TimeUtils.YearMonth(2017,2018);
        for (int i = 0; i < monthlist.size(); i++) {
            System.out.println(monthlist.get(i));
        }
        String time = getMonth("2002-1-08 14:50:38");
        System.out.println(time);
        System.out.println(getDay("2002-1-08 14:50:38"));
        System.out.println(TimeUtils.parseTime("2016-05-19 19:17","yyyy-MM-dd HH:mm"));
        String data=getNowMonth();
        System.out.println(data);
    }
    //獲取當前時間
    public static String GetNowDate(String formate){  
        String temp_str="";  
        Date dt = new Date();  
        SimpleDateFormat sdf = new SimpleDateFormat(formate);  
        temp_str=sdf.format(dt);  
        return temp_str;  
    }  
    //獲取當前月
    public static String getMonth( String time ){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date = null;
        try {

            date = sdf.parse(time);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return sdf.format(date);

    }
    //獲取當前日期
    public static String getDay( String time ){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {

            date = sdf.parse(time);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return sdf.format(date);

    }
    //輸入時間,解析成"yyyy-MM-dd HH:mm:ss"格式
    public static Date parseTime(String inputTime) throws ParseException{

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        Date date = sdf.parse(inputTime); 

        return date;

    }
    //將時間轉成成字元型
    public static String dateToString(Date date, String type) { 
        DateFormat df = new SimpleDateFormat(type);  
        return df.format(date);  
    }
    //將輸入的時間,轉化成指定格式
    public static Date parseTime(String inputTime, String timeFormat) throws ParseException{

        SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);  
        Date date = sdf.parse(inputTime); 

        return date;

    }

    public static Calendar parseTimeToCal(String inputTime, String timeFormat) throws ParseException{

        SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);  
        Date date = sdf.parse(inputTime); 
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        return calendar;

    }

    public static int getDaysBetweenCals(Calendar cal1, Calendar cal2) throws ParseException{

        return (int) ((cal2.getTimeInMillis()-cal1.getTimeInMillis())/(1000*24*3600));

    }
    //長整型轉化為時間
    public static Date parseTime(long inputTime){

        //  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date= new Date(inputTime);
        return date;

    }

    public static String parseTimeString(long inputTime){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date= new Date(inputTime);
        return sdf.format(date);

    }
    public static String parseStringTime(String inputTime){

        String date=null;
        try {
            Date date1 = new SimpleDateFormat("yyyyMMddHHmmss").parse(inputTime);
            date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date1);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return date;
    }
    public static List<String> YearMonth(int year) {
        List<String> yearmouthlist=new ArrayList<String>();
        for (int i = 1; i < 13; i++) {
            DecimalFormat dfInt=new DecimalFormat("00");
            String sInt = dfInt.format(i);
            yearmouthlist.add(year+sInt);
        }

        return yearmouthlist;
    } 
    //獲取從起始年份到目標年份所有的月
    public static List<String> YearMonth(int startyear,int finistyear) {
        List<String> yearmouthlist=new ArrayList<String>();
        for (int i = startyear; i < finistyear+1; i++) {
            for (int j = 1; j < 13; j++) {
                DecimalFormat dfInt=new DecimalFormat("00");
                String sInt = dfInt.format(j);
                yearmouthlist.add(i +""+sInt);
            }
        }
        return yearmouthlist;
    } 
    public static List<String> TOAllDay(int year){
        List<String> daylist=new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
        int m=1;//月份計數 
        while (m<13) 
        { 
            int month=m; 
            Calendar cal=Calendar.getInstance();//獲得當前日期物件 
            cal.clear();//清除資訊 
            cal.set(Calendar.YEAR,year); 
            cal.set(Calendar.MONTH,month-1);//1月從0開始 
            cal.set(Calendar.DAY_OF_MONTH,1);//設定為1號,當前日期既為本月第一天  

            System.out.println("##########___" + sdf.format(cal.getTime())); 

            int count=cal.getActualMaximum(Calendar.DAY_OF_MONTH); 

            System.out.println("$$$$$$$$$$________" + count); 

            for (int j=0;j<=(count - 2);) 
            { 
                cal.add(Calendar.DAY_OF_MONTH,+1); 
                j++; 
                daylist.add(sdf.format(cal.getTime()));
            } 
            m++; 
        } 
        return daylist;
    }
    //獲取昨天的日期
    public static String getyesterday(){
        Calendar   cal   =   Calendar.getInstance();
        cal.add(Calendar.DATE,   -1);
        String yesterday = new SimpleDateFormat( "yyyy-MM-dd ").format(cal.getTime());
        return yesterday;
    }
    //獲取當前年份月份
    public static String getNowMonth(){
        Calendar   cal   =   Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);    
        int month = cal.get(Calendar.MONTH) + 1; 
        DecimalFormat dfInt=new DecimalFormat("00");
        String sInt = dfInt.format(month);
        return year+""+sInt;
    }
}

相關文章