zoukankan      html  css  js  c++  java
  • 安卓时间类型的转换和比大小

    在Android开发过程中,经常会遇到日期的各种格式转换,主要使用SimpleDateFormat这个类来实现,掌握了这个类,可以转换任何你想要的各种格式。

    常见的日期格式:

    1,日期格式:String dateString = "2017-06-20 10:30:30" 对应的格式:String pattern = "yyyy-MM-dd HH:mm:ss";

    2,日期格式:String dateString = "2017-06-20" 对应的格式:String pattern = "yyyy-MM-dd";

    3,日期格式:String dateString = "2017年06月20日 10时30分30秒 对应的格式:String pattern = "yyyy年MM月dd日 HH时mm分ss秒";

    4,日期格式:String dateString = "2017年06月20日" 对应的格式:String pattern = "yyyy年MM月dd日";

    一、将长时间格式时间转换为字符串 yyyy.MM.dd HH:mm

    public static String dateToStrToHours(Date dateDate) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd HH:mm");
            String dateString = formatter.format(dateDate);
            return dateString;
        }

    注:yyyy-MM-dd HH:mm:ss需要写到哪就写到哪,比如以上这个就是取到到分钟(下面不在赘述)

    二、将长时间格式时间字符串 yyyy年MM月dd日HH时转换为Date

    public static Date strToDateLong(String strDate) {
            String format = null;
            Date date = null;
            try {
                SimpleDateFormat formatter_old = new SimpleDateFormat("yyyy年MM月dd日HH时");
                date = formatter_old.parse(strDate);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return date;
        }

    三、long类型转换为String类型

        // currentTime要转换的long类型的时间
        // formatType要转换的string类型的时间格式
        public static String longToString(long currentTime, String formatType)
                throws ParseException {
            Date date = longToDate(currentTime, formatType); // long类型转成Date类型
            String strTime = dateToString(date, formatType); // date类型转成String
            return strTime;
        }

     这里的formatType也就是上面定义的四种类型

    四、long转换为Date类型

    // currentTime要转换的long类型的时间
     // formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
     public static Date longToDate(long currentTime, String formatType)
     throws ParseException {
     Date dateOld = new Date(currentTime);
     String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
     Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
     return date;
     }

    五、String类型转换为long类型

        // strTime要转换的String类型的时间
        // formatType时间格式
        // strTime的时间格式和formatType的时间格式必须相同
        public static long stringToLong(String strTime, String formatType)
                throws ParseException {
            Date date = stringToDate(strTime, formatType); // String类型转成date类型
            if (date == null) {
                return 0;
            } else {
                long currentTime = dateToLong(date); // date类型转成long类型
                return currentTime;
            }
        }

    六、比较两个时间打大小

    比较两个日期的大小,日期格式为yyyy-MM-dd
    
    
    /**
    * 比较两个日期的大小,日期格式为yyyy-MM-dd
    *
    * @param str1 the first date
    * @param str2 the second date
    * @return t

    public
    static boolean isDateOneBigger1(String str1, String str2) { boolean isBigger = false; SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm"); Date dt1 = null; Date dt2 = null; try { dt1 = sdf.parse(str1); dt2 = sdf.parse(str2); } catch (ParseException e) { e.printStackTrace(); } if (dt1.getTime() > dt2.getTime()) { isBigger = false; } else if (dt1.getTime() < dt2.getTime()) { isBigger = true; } return isBigger; }

    需要查看第一、三点转为string 然后进行比较。

    以上方法直接用。

                                                                   

    --磊tua

  • 相关阅读:
    TOEFL资料 280多个
    Eclipse搭建J2ME开发环境
    Session.Abandon和Session.Clear有何不同
    进程之同步、互斥PV操作笔记
    Windows Mobile 6.5 实现联系人分组显示
    关于数据库的版本控制
    xhtml的布局,满屏,高度自适应
    MOSS 项目模板
    DNN中与模块相关的信息
    J2EE学习笔记
  • 原文地址:https://www.cnblogs.com/widgetbox/p/8393438.html
Copyright © 2011-2022 走看看