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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早起的年轻人

创作源于分享

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值