博客
关于我
Android关于时间日期相关常用方法
阅读量:792 次
发布时间:2019-03-24

本文共 3635 字,大约阅读时间需要 12 分钟。

public class DateUtils {
/**
* 获取当前时间的毫秒数
* 参数格式:yyyy-MM-dd HH:mm:ss
* @param time
* @return
* @throws ParseException
*/
public static long getTimeInMillis(String time) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(time));
long timeInMillis = cal.getTimeInMillis();
return timeInMillis;}
/**
* 获取当前日期格式: yyyy-mm-dd HH:mm:ss
*/
public static String getDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(new Date());}
/**
* 获取当前日期格式: yyyy-mm-dd
*/
public static String getDate1() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new Date());}
/**
* 获取当前时间格式: HH:mm:ss
*/
public static String getTime() {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
return sdf.format(new Date());}
/**
* 获取当前时间格式: yyyyMMddHHmmss
*/
public static String getTime2() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.format(new Date());}
/**
* 获取当前星期几
*/
public static String getWeek() {
Calendar c = Calendar.getInstance();
int mWay = c.get(Calendar.DAY_OF_WEEK);
if (mWay == 1) return "星期天";
else if (mWay == 2) return "星期一";
else if (mWay == 3) return "星期二";
else if (mWay == 4) return "星期三";
else if (mWay == 5) return "星期四";
else if (mWay == 6) return "星期五";
else return "星期六";}
/**
* 计算两个日期之间的相差天数
* @param smdate 较小的时间
* @param bdate 较大的时间
* @return 天数差
* @throws ParseException
*/
public static int daysBetween(Date smdate, Date bdate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
smdate = sdf.parse(sdf.format(smdate));
bdate = sdf.parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return (int) between_days;}
/**
* 计算两个时间之间的天数
*@param smdate 较小的时间字符串
* @param bdate 较大的时间字符串
* @return 天数差
* @throws ParseException
*/
public static int daysBetweenString(String smdate, String bdate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return (int) between_days;}
/**
* 判断当前时间是否在给定时间段内
* @param currentTime 当前时间字符串
* @param beginTime 开始时间字符串
* @param endTime 结束时间字符串
* @return boolean
*/
public static boolean isCurrentTimeBetween(String currentTime, String beginTime, String endTime) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(currentTime));
long currentTimeMillis = cal.getTimeInMillis();
cal.setTime(sdf.parse(beginTime));
long beginTimeMillis = cal.getTimeInMillis();
cal.setTime(sdf.parse(endTime));
long endTimeMillis = cal.getTimeInMillis();
return currentTimeMillis > beginTimeMillis && currentTimeMillis < endTimeMillis;}

以上代码经过优化后,内容更加简洁自然,突出了主要功能,同时增加了适当的描述,使页面更易于搜索引擎索引。

转载地址:http://nbuuk.baihongyu.com/

你可能感兴趣的文章
Mysql 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>