Date,DateTime,String等日期格式的相互转化

来源:互联网 发布:ubuntu传文件 编辑:程序博客网 时间:2024/05/22 09:38
日期之间的转化关系:        1,获取当前系统时间   Date date = new Date();2Date转为DateTime   DateTime dateTime = new DateTime(date.getTime());3DateTime转为Date   Date date = dateTime.toDate();4,获取日期格式,   DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");5Date转换为String类型格式   String dateStr = df.format(new Date());工具类/** * 返回当前日期时间字符串<br> * 默认格式:yyyy-mm-dd hh:mm:ss * * @return String 返回当前字符串型日期时间 */public static String getCurrentTime() {        String returnStr = null;        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        Date date = new Date();        returnStr = f.format(date);        return returnStr;        }/** * 返回当前日期时间字符串<br> * 默认格式:yyyymmddhhmmss * * @return String 返回当前字符串型日期时间 */public static BigDecimal getCurrentTimeAsNumber() {        String returnStr = null;        SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");        Date date = new Date();        returnStr = f.format(date);        return new BigDecimal(returnStr);        }/** * 返回自定义格式的当前日期时间字符串 * * @param format *            格式规则 * @return String 返回当前字符串型日期时间 */public static String getCurrentTime(String format) {        String returnStr = null;        SimpleDateFormat f = new SimpleDateFormat(format);        Date date = new Date();        returnStr = f.format(date);        return returnStr;        }/** * 返回当前字符串型日期 * * @return String 返回的字符串型日期 */public static String getCurDate() {        Calendar calendar = Calendar.getInstance();        SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd");        String strDate = simpledateformat.format(calendar.getTime());        return strDate;        }/** * 返回指定格式的字符型日期 * @param date * @param formatString * @return */public static String Date2String(Date date, String formatString) {        if (G4Utils.isEmpty(date)) {        return null;        }        SimpleDateFormat simpledateformat = new SimpleDateFormat(formatString);        String strDate = simpledateformat.format(date);        return strDate;        }/** * 返回当前字符串型日期 * * @param format *            格式规则 * * @return String 返回的字符串型日期 */public static String getCurDate(String format) {        Calendar calendar = Calendar.getInstance();        SimpleDateFormat simpledateformat = new SimpleDateFormat(format);        String strDate = simpledateformat.format(calendar.getTime());        return strDate;        }/** * 返回TimeStamp对象 * * @return */public static Timestamp getCurrentTimestamp() {        Object obj = TypeCaseHelper.convert(getCurrentTime(), "Timestamp", "yyyy-MM-dd HH:mm:ss");        if (obj != null)        return (Timestamp) obj;        else        return null;        }/** * 将字符串型日期转换为日期型 * * @param strDate *            字符串型日期 * @param srcDateFormat *            源日期格式 * @param dstDateFormat *            目标日期格式 * @return Date 返回的util.Date型日期 */public static Date stringToDate(String strDate, String srcDateFormat, String dstDateFormat) {        Date rtDate = null;        Date tmpDate = (new SimpleDateFormat(srcDateFormat)).parse(strDate, new ParsePosition(0));        String tmpString = null;        if (tmpDate != null)        tmpString = (new SimpleDateFormat(dstDateFormat)).format(tmpDate);        if (tmpString != null)        rtDate = (new SimpleDateFormat(dstDateFormat)).parse(tmpString, new ParsePosition(0));        return rtDate;        }/** * dppx * * @param context * @param * @return */public static float dp2px(Context context, int dpVal) {        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,        dpVal, context.getResources().getDisplayMetrics());        }/** * sppx * * @param context * @param spVal * @return */public static float sp2px(Context context, float spVal) {        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,        spVal, context.getResources().getDisplayMetrics());        }/** * 屏幕宽度 * * @param context * @return */public static int getDisplayWidth(Context context) {        return context.getResources().getDisplayMetrics().widthPixels;        }/** * 屏幕高度 * * @param context * @return */public static int getDisplayHeight(Context context) {        return context.getResources().getDisplayMetrics().heightPixels;        }//是否同月public static boolean isEqualsMonth(DateTime dateTime1, DateTime dateTime2) {        return dateTime1.getMonthOfYear() == dateTime2.getMonthOfYear();        }/** * 第一个是不是第二个的上一个月,只在此处有效 * * @param dateTime1 * @param dateTime2 * @return */public static boolean isLastMonth(DateTime dateTime1, DateTime dateTime2) {        DateTime dateTime = dateTime2.plusMonths(-1);        return dateTime1.getMonthOfYear() == dateTime.getMonthOfYear();        }/** * 第一个是不是第二个的下一个月,只在此处有效 * @param dateTime1 * @param dateTime2 * @return */public static boolean isNextMonth(DateTime dateTime1, DateTime dateTime2) {        DateTime dateTime = dateTime2.plusMonths(1);        return dateTime1.getMonthOfYear() == dateTime.getMonthOfYear();        }/** * 获得两个日期距离几个月 * @return */public static int getIntervalMonths(DateTime dateTime1, DateTime dateTime2) {        return (dateTime2.getYear() - dateTime1.getYear()) * 12 + (dateTime2.getMonthOfYear() - dateTime1.getMonthOfYear());        }/** * 获得两个日期距离几周 * * @param dateTime1 * @param dateTime2 * @return */public static int getIntervalWeek(DateTime dateTime1, DateTime dateTime2) {        DateTime sunFirstDayOfWeek1 = getSunFirstDayOfWeek(dateTime1);        DateTime sunFirstDayOfWeek2 = getSunFirstDayOfWeek(dateTime2);        int days = Days.daysBetween(sunFirstDayOfWeek1, sunFirstDayOfWeek2).getDays();        if (days > 0) {        return (days + 1) / 7;        } else if (days < 0) {        return (days - 1) / 7;        } else {        return days;        }        }/** * 是否是今天 * @param dateTime * @return */public static boolean isToday(DateTime dateTime) {        return new DateTime().toLocalDate().equals(dateTime.toLocalDate());        }/** * 某月第一天是周几 * * @return */public static int getFirstDayOfWeekOfMonth(int year, int month) {        int dayOfWeek = new DateTime(year, month, 1, 0, 0, 0).getDayOfWeek();        if (dayOfWeek == 7) {        return 0;        }        return dayOfWeek;        }//转化一周从周日开始public static DateTime getSunFirstDayOfWeek(DateTime dateTime) {        if (dateTime.dayOfWeek().get() == 7) {        return dateTime;        } else {        return dateTime.minusWeeks(1).withDayOfWeek(7);        }        }/** * 通过年份和月份 得到当月的日子 * * @param year * @param month * @return */public static int getMonthDays(int year, int month) {        month++;        switch (month) {        case 1:        case 3:        case 5:        case 7:        case 8:        case 10:        case 12:        return 31;        case 4:        case 6:        case 9:        case 11:        return 30;        case 2:        if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {        return 29;        } else {        return 28;        }default:        return -1;        }        }/** * 获取这个月有几行 * * @param year * @param month * @return */public static int getMonthRows(int year, int month) {        int size = getFirstDayOfWeekOfMonth(year, month) + getMonthDays(year, month) - 1;        return size % 7 == 0 ? size / 7 : (size / 7) + 1;        }/** * 返回当前月份1号位于周几 * * @param year  年份 * @param month 月份,传入系统获取的,不需要正常的 * @return 日:1    一:2       二:3       三:4       四:5       五:6       六:7 */public static int getFirstDayWeek(int year, int month) {        Calendar calendar = Calendar.getInstance();        calendar.set(year, month, 1);        return calendar.get(Calendar.DAY_OF_WEEK);        }public static int getYearByTimeStamp(long timeStamp) {        String date = timeStampToDate(timeStamp);        String year = date.substring(0, 4);        return Integer.parseInt(year);        }public static String timeStampToDate(long timeStamp) {        Date date = new Date(timeStamp);        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        String dateStr = simpleDateFormat.format(date);        return dateStr;        }/** * 获取当前日期是星期几<br> * * @param dt * @return 当前日期是星期几 */public static Integer getWeekOfDate(DateTime dt) {//        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};        Integer[] weekDays = {0, 1, 2, 3, 4, 5, 6};        Calendar cal = Calendar.getInstance();        cal.setTime(dt.toDate());        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;        if (w < 0)        w = 0;        return weekDays[w];        }/** * 获取一个日期加上天数得到最后的日期 * * @param d   日期 * @param day 加的天数 如果天数是负数,即是减 * @return 当前日期是星期几 */public static Date addDate(Date d, long day) {        long time = d.getTime();        day = day * 24 * 60 * 60 * 1000;        time += day;        return new Date(time);        }

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 开拼多多店铺的密码忘了怎么办 拼多多密码跟店铺名忘了怎么办 闲鱼上卖出的宝贝被调包了怎么办 上传身份证照片说格式错误该怎么办 我给厂里打的款不给发货怎么办 净值接近不定期份额折算阀值怎么办 有锁电信4g掉了怎么办 在电脑中找不到想作废的发票怎么办 科目三补考费发票丢了怎么办 母婴店飞鹤奶粉突然厂家撤货怎么办 澳门买的保健品感觉是假的怎么办 淘宝买东西提交需求时卡死了怎么办 天猫精灵显示为离线状态怎么办 退货多被淘宝店铺拉入黑名单怎么办 天猫订单3天不发货怎么办 新开的厨卫店越来越没生意怎么办 淘宝积分不够领不到购物津贴怎么办 穿越火线精英集结号积分不足怎么办 天猫购物津贴领多了怎么办 车贷逾期车被开走还不清全款怎么办 孩子特别懒不爱动又胖怎么办 微信的聊天记录被限制了怎么办 门面租金交了一年对方不租了怎么办 离职后社保怎么办 无忧保专业可靠 淘宝店铺被投诉到监管局怎么办 淘宝买家每天都来店铺骚扰怎么办 电脑安装软件时解压出现问题怎么办 公婆不尊重你的父母做媳妇的怎么办 手机扣扣接收不上文件怎么办 买了商铺付了首付商铺倒闭了怎么办 宝宝刚满月不喜欢在床上睡怎么办 别人给我打了收货款不发货怎么办 业务员私收货款公司不发货怎么办 付款后商家没发货也不退钱怎么办 苹果7P修过主板耗电严重怎么办 oppo手机进水了开不了机怎么办 手机弯了变形但不影响使用怎么办 拼多多留错电话怎么办如果已经发货 苹果6手机后壳变形了怎么办 京东退货保修卡丢了怎么办 如果京东买的显示器屏碎了怎么办