JAva日付時間各種変換及び処理

13739 ワード

本文の著者:合肥工業大学管理学院銭洋email:[email protected]内容に不備があるかもしれませんが、交流を歓迎します.本人の許可なく転載を禁止する.
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:[email protected]
 * @ 
 */
public class TimeUtils {

    public static void main( String[] args ) throws ParseException{
        List 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 YearMonth(int year) {
        List yearmouthlist=new ArrayList();
        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 YearMonth(int startyear,int finistyear) {
        List yearmouthlist=new ArrayList();
        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 TOAllDay(int year){
        List daylist=new ArrayList();
        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;
    }
}