zoukankan      html  css  js  c++  java
  • Android基础常用日期操作工具类

    将指定日期格式转换为毫秒(一)

    public class DataUtil {
    
        /**
         * "2015-01-01" 将指定的日期格式转换为毫秒
         * @param time
         * @return
         */
        public static long  getStringToTime(String time){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                return sdf.parse(time).getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return 0;
        }
    }

    将毫秒数转换成指定格式日期

    	public static String timeToDate(long string){
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
    		return sdf.format(string);
    		
    	}

    将long类型的毫秒值转换成固定日期格式

           private String toData(long string) {
    		if (string == 0) {
    			return "";
    		}
    		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd  hh:mm");
    		Calendar calendar = Calendar.getInstance();
    		calendar.setTimeInMillis(string);
    		return formatter.format(calendar.getTime());
    	}

    将固定日期转换成long类型的毫秒值(二)

    private long getEditeViewResultTime(TextView mEtItem82) {
    		String trim = mEtItem82.getText().toString();
    		long millionSeconds = 0;
    		if (!TextUtils.isEmpty(trim)) {
    			
    			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  hh:mm");
    
    			try {
    				millionSeconds = sdf.parse(trim).getTime();
    
    			} catch (ParseException e) {
    				e.printStackTrace();
    			} // 毫秒
    		}
    
    		return millionSeconds;
    	}

    将固定的日期转换

    private String toDateString(String time) {
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  hh:mm");
    		Date date = new Date(time);
    		String str = sdf.format(date);
    		return str;
    	}

    public class DateUtils {
    
        public static String toDate(Date date) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
            return dateFormat.format(date);
        }
    
        public static Date getLastdayDate(Date date) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE, -1);
            return calendar.getTime();
        }
    
        public static Date getNextdayDate(Date date) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE, 1);
            return calendar.getTime();
        }




  • 相关阅读:
    rotate list
    使用存取方法来设置Property value
    模拟创建类变量,static变量加类方法,单例
    下拉刷新常规代码
    强制横竖屏间切换
    友盟分享
    快速下拉刷新动画
    把电脑上的视频导入苹果6
    xcrun: error: active developer path ("/Volumes/Xcode/Xcode-beta.app/Contents/Developer") does not exist, use `xcode-select --swi
    iOS 刚刚,几分钟前,几小时前,几天前,几月前,几年前
  • 原文地址:https://www.cnblogs.com/miaozhenzhong/p/5930928.html
Copyright © 2011-2022 走看看