zoukankan      html  css  js  c++  java
  • Java时间转换类实现

    Java时间类型非常的差,首先版本问题,本人使用java在Android平台开发。很多Data类的方法已经不提倡使用,一个时间,居然要使用Calendar、DateFormat等类共同编码,非常麻烦。而且很多支持不足,比如时间加减,比如获取时间的日期而时分秒置0等,而C#的时间类实现的就非常方便。

    public class DateUtil {
        public static Date getDate(Date date){
            Date dt = null;
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.HOUR_OF_DAY,   0);   
            calendar.set(Calendar.MINUTE,   0);   
            calendar.set(Calendar.SECOND,   0);   
            calendar.set(Calendar.MILLISECOND,   0);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            dt = calendar.getTime();
            return dt;
        }
        
        public static String getDateString(Date date){
            String sDate = "";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    
            Date dt = getDate(date);
            sDate = sdf.format(dt);
            
            return sDate;
        }
        
        public static long getDateLong(Date date){
            long lDate = 0;
            Date dt = getDate(date);
            lDate = dt.getTime();
            return lDate;
        }
        
        public static long getDateTimeLong(Date date){
            long lDate = 0;
            lDate = date.getTime();
            return lDate;
        }
        
        public static String getString(Date date, String format){
            String sDate ="";
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            sDate = sdf.format(date);
            return sDate;
        }
        
        public static long getString(String dateTime, String format){
            long lDate = 0;
            
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            try {
                Date date = sdf.parse(dateTime);
                lDate = date.getTime();
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            return lDate;
        }
        
        public static String getString(long tm, String format){
            String sDate="";
            Date date = DateUtil.putDateTime(tm);
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            sDate = sdf.format(date);
            return sDate;
        }
        
        public static Date putDateTime(long time){
            Date date = null;
            date = new Date(time);
            return date;
        }
        
        public static Date putDate(long time){
            Date date = null;
            date = new Date(time);
            date = getDate(date);
            return date;
        }
    
        public static Date putDateTime(String dtString, String format){
            Date date = null;
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            try {
                date = sdf.parse(dtString);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return date;
        }
    
    
        public static Date AddYear(Date date, int y){
            Date dt = null;
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.YEAR, y);
            dt = calendar.getTime();
            return dt;
        }
        public static Date AddMonth(Date date, int m){
            Date dt = null;
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, m);
            dt = calendar.getTime();
            return dt;
        }
        public static Date AddDay(Date date, int d){
            Date dt = null;
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, d);
            dt = calendar.getTime();
            return dt;
        }
        public static Date AddHour(Date date, int h){
            Date dt = null;
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.HOUR_OF_DAY, h);
            dt = calendar.getTime();
            return dt;
        }
        public static Date AddMinute(Date date, int m){
            Date dt = null;
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MINUTE, m);
            dt = calendar.getTime();
            return dt;
        }
        public static Date AddSecond(Date date, int s){
            Date dt = null;
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.SECOND, s);
            dt = calendar.getTime();
            return dt;
        }
        
        public static Date Add(Date date, int millisecond){
            Date dt = null;
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MILLISECOND, millisecond);
            dt = calendar.getTime();
            return dt;
        }
        public static long Subtrat(Date date1, Date date2){
            long l = 0;
            l = date1.getTime() - date2.getTime();
            return l;
        }
    
    }
  • 相关阅读:
    Starting Tomcat v7.0 Server at localhost (2)hasencountered a problemServer Tomcat v7.0 Server at localhost (2)failed tostart
    如何获取系统当前时间
    解决TextEncoder 和 TextDecoder在IE下不兼容 vue 用iconv-lite插件代替 解决中文乱码问题
    vue 读取本地TXT GBK编码文件
    HTML常用标签和属性大全
    echarts中的个性化设计
    MySQL常用优化指南和思路
    微服务框架 Service Mesh
    spring boot actuator监控
    关于Swagger @ApiModel 返回内容注释不显示问题
  • 原文地址:https://www.cnblogs.com/fyhui/p/3302663.html
Copyright © 2011-2022 走看看