常用的时间格式

/**
 * 时间格式工具类
 */
public final class TimeUtils {
    /**
     * 时间格式: yyyy-MM-dd HH:mm:ss
     */
    public static String getStandardTimeToSecond(){
        Calendar calendar=Calendar.getInstance();
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=sdf.format(calendar.getTime());
        return time;
    }
    /**
     * 时间格式:yyyy-MM-dd HH:mm
     */
    public static String getStandardTimeToMinute(){
        Calendar calendar=Calendar.getInstance();
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String time=sdf.format(calendar.getTime());
        return time;
    }
    /**
     * 时间格式:yyyy.MM.dd
     */
    public static String getStandardTimeToDay(){
        Calendar calendar=Calendar.getInstance();
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy.MM.dd");
        String time=sdf.format(calendar.getTime());
        return time;
    }
    /**
     * 返回时间差;格式:00小时00分钟00秒
     */
    public static  String getTimeDifferent(String time1,String time2){
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long getTime1 = 0;
        long getTime2 = 0;
        try {
            getTime1 = sdf.parse(time1).getTime();
            getTime2 = sdf.parse(time2).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long timeDifferent=getTime2-getTime1;
        SimpleDateFormat getSdf=new SimpleDateFormat("HH小时mm分ss秒");
        //设置时区
        getSdf.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
        String getTimeDifferent=getSdf.format(timeDifferent);
        return getTimeDifferent;
    }
}

你可能感兴趣的:(Java基础学习笔记,java)