博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java时间转换类实现
阅读量:6820 次
发布时间:2019-06-26

本文共 4635 字,大约阅读时间需要 15 分钟。

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;    }}

 

转载于:https://www.cnblogs.com/fyhui/p/3302663.html

你可能感兴趣的文章
mysql 创建日期列之timestamp
查看>>
VMM系列之使用VMM服务器构建 Hyper-V主机(4)
查看>>
详测 Generics Collections TList (7): Items、Contains
查看>>
配置FTP服务器(2) 本地用户下载和上传
查看>>
多线程编程(11) - 多线程同步之 Mutex (互斥对象)[续]
查看>>
【Java每日一题】20161214
查看>>
requireJs 模块化简陋版本
查看>>
我的友情链接
查看>>
How to upgrade vim to version 8 on CentOS 7
查看>>
xcode pod 报import 找不到 pods的支持问题解决方法之一
查看>>
nginx配置让任何文件在浏览器中显示文本text/plain
查看>>
思科路由器×××配置-- 动态 site-to-site ×××(上)
查看>>
Visual Studio统计有效代码行数
查看>>
Qt连接Oracle数据库常见问题
查看>>
45个实用的JavaScript技巧、窍门和最佳实践
查看>>
sqlserver 2005 列字符串拼接
查看>>
用面向接口编程思想看找对象
查看>>
TWaver GIS在电信中的使用
查看>>
MySQL5.7使用Notifier启动、停止服务时出现的问题
查看>>
今天用java弄个json数据交换接口,个人感觉这样实现省事实力。
查看>>