zoukankan      html  css  js  c++  java
  • Android:Date、String、Long三种日期类型之间的相互转换

    源地址:http://blog.csdn.net/wangyanguiyiyang

    date类型转换为String类型:

    // formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
        // data Date类型的时间
        public static String dateToString(Date data, String formatType) {
            return new SimpleDateFormat(formatType).format(data);
        }

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

    string类型转换为date类型:

    // strTime要转换的string类型的时间,formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
        // HH时mm分ss秒,
        // strTime的时间格式必须要与formatType的时间格式相同
        public static Date stringToDate(String strTime, String formatType)
                throws ParseException {
            SimpleDateFormat formatter = new SimpleDateFormat(formatType);
            Date date = null;
            date = formatter.parse(strTime);
            return date;
        }

    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); // 根据long类型的毫秒数生命一个date类型的时间
     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;
            }
        }

    date类型转换为long类型

     // date要转换的date类型的时间
        public static long dateToLong(Date date) {
            return date.getTime();
        }
  • 相关阅读:
    漂亮灵活设置的jquery通知提示插件toastr
    C# 对List<T>取交集、连集及差集
    查看sqlserver被锁的表以及如何解锁.
    javascript的setTimeout()用法总结,js的setTimeout()方法
    日志插件 log4net 的使用
    Log4NET简介
    解决WCF大数据量传输 ,System.Net.Sockets.SocketException: 远程主机强迫关闭了一个现有的连接
    也说Autofac在MVC的简单实践:破解在Controller构造函数中的实例化
    Autofac 依赖注入 ASP.NET MVC5 插件机制中插件的简单实现
    Asp.net mvc中整合autofac
  • 原文地址:https://www.cnblogs.com/wkmocr/p/7762295.html
Copyright © 2011-2022 走看看