Android 时间与日期操作类
获取本地日期与时间
public String getCalendar() {
@SuppressLint("SimpleDateFormat")
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date dt = new Date();
return sdf.format(dt);
}
//SimpleDateFormat 时间格式
//y 年 MM 月 dd 日
//D 一年的第几天 W 一个月的第几星期 w 一年的第几星期 k时 z时区
//HH 小时(24小时制) hh小时(12小时制) mm 分 ss 秒 SS 毫秒 E 星期
//a 上午/下午
计算相隔天数
/**
* 获得天数差
* @param begin
* @param end
* @return
*/
public long getDayDiff(Date begin, Date end){
long day = 1;
if(end.getTime() < begin.getTime()){
day = -1;
}else if(end.getTime() == begin.getTime()){
day = 1;
}else {
day += (end.getTime() - begin.getTime())/(24 * 60 * 60 * 1000) ;
}
return day-1;
}
Date date=new Date(2017-1900,11,1,0,30);
Date date2=new Date(2018-1900,11,1,0,30);
getDayDiff(date,date2)
计算时间差
public static String getTimeDiffer(String date,String date2,String str){
long diff,days,hours,minutes;
@SuppressLint("SimpleDateFormat")
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try
{
Date d1 = df.parse(date);
Date d2 = df.parse(date2);
if(d1.getTime()>d2.getTime()) {
diff = d1.getTime() - d2.getTime();//这样得到的差值是毫秒级别
days = diff / (1000 * 60 * 60 * 24);
hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
}else{
return "已到期";
}
if(days==0){
if(hours==0){
return minutes+"分钟";
}
}
if(str.equals("精确")){
return ""+days+"天"+hours+"小时"+minutes+"分钟";
}
return ""+days+"天"+hours+"小时";
}catch (Exception ignored){
}
return "";
}
判断日期是否在指定日期
public static int getTimeCompareSize2(String startTime, String endTime){
int i=0;
@SuppressLint("SimpleDateFormat")
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");//年-月-日 时-分
try {
Date date1 = dateFormat.parse(startTime);//开始时间
Date date2 = dateFormat.parse(endTime);//结束时间
if (date2.getTime()<date1.getTime()){
i= 1;// 结束时间小于开始时间
}else if (date2.getTime()==date1.getTime()){
i= 2;//开始时间与结束时间相同
}else if (date2.getTime()>date1.getTime()){
i= 3;//结束时间大于开始时间
}
} catch (ParseException e) {
e.printStackTrace();
}
return i;
}
获取网络时间与日期
private void getNetTime() {
URL url = null;
try {
url = new URL("http://www.baidu.com");
URLConnection uc = url.openConnection();
uc.connect();
long ld = uc.getDate(); //取得网站日期时间
@SuppressLint("SimpleDateFormat")
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(ld);
final String format = formatter.format(calendar.getTime());
boottom_tiem.setText(format);
} catch (Exception e) {
e.printStackTrace();
}
}
Android 时间与日期操作类的更多相关文章
- Android随笔之——Android时间、日期相关类和方法
今天要讲的是Android里关于时间.日期相关类和方法.在Android中,跟时间.日期有关的类主要有Time.Calendar.Date三个类.而与日期格式化输出有关的DateFormat和Simp ...
- 使用日期操作类(Calendar)获得几秒、几分钟、几小时之前的时间
public String dealDate(String case_time){ // 日期操作类 Calendar calendar = Calendar.getInstance(); // 当前 ...
- JAVA笔记10__Math类、Random类、Arrays类/日期操作类/对象比较器/对象的克隆/二叉树
/** * Math类.Random类.Arrays类:具体查JAVA手册...... */ public class Main { public static void main(String[] ...
- 菜鸡的Java笔记 日期操作类
日期操作类 Date 类与 long 数据类型的转换 SimpleDateFormat 类的使用 Calendar 类的使用 如 ...
- Lua库之时间和日期操作
Lua库之时间和日期操作 (2010-02-07 18:41:20) 转载▼ os.time() <== 返回当前系统的日历时间os.date() <== 返回本地化的时间字符串,这里是& ...
- 日期操作类--Date类
Date-API ava.util包提供了Date类来封装当前的日期和时间.Date类提供两个构造函数来实例化Date对象.第一个构造函数使用当前日期和时间来初始化对象. Date( ) 第二个构造函 ...
- 日期操作类--GregorianCalendar类
GregorianCalendar--API JavaTM Platform Standard Ed. 6 GregorianCalendar类 Calendar类实现了公历日历,GregorianC ...
- 日期操作类--SimpleDateFormat类
使用SimpleDateFormat格式化日期 SimpleDateFormat是一个以语言环境敏感的方式来格式化和分析日期的类.SimpleDateFormat允许你选择任何用户自定义日期时间格式来 ...
- 日期操作类--DateFormat类
简单的DateFormat格式化编码 时间模式字符串用来指定时间格式.在此模式中,所有的ASCII字母被保留为模式字母,定义如下: 字母 描述 示例 G 纪元标记 AD y 四位年份 2001 M 月 ...
随机推荐
- Java学习笔记34(集合框架八:综合案例:模拟斗地主的洗牌发牌)
规则: 1.54张扑克牌,有花色 2.顺序打乱,一人一张依次发牌,一人17张,留三张作为底牌 3.看牌:按大小王2A....43的序排列打印 示例: package demo; import java ...
- linux 软件安装篇
在linux下安装软件,不像windows一样,下一步下一步安装,但是也有很方便的方式.也有自定义的安装方式,总体来说,套路还不算太深,但是要实践才能出真知哦! linux版本有很多,但是大部分命令都 ...
- MySQL slow_log表不能修改成innodb引擎
背景 从mysql.slow_log 获取慢查询日志很慢,该表是csv表,没有索引. 想添加索引来加速访问,而csv引擎不能添加索引(csv引擎存储是以逗号分割的文本来存储的),只能改存储引擎来添加索 ...
- LeetCode:149_Max Points on a line | 寻找一条直线上最多点的数量 | Hard
题目:Max Points on a line Given n points on a 2D plane, find the maximum number of points that lie on ...
- String-intern方法举例分析其含义
之后重新理解这个知识点时,又写了一些小例子 String a1 = new String("str01") ; String a2 = "str01" ; Sy ...
- LDA-线性判别分析(三)推广到 Multi-classes 情形
本来是要调研 Latent Dirichlet Allocation 的那个 LDA 的, 没想到查到很多关于 Linear Discriminant Analysis 这个 LDA 的资料.初步看了 ...
- php实现聊天室功能
原理:长连接 一.长连接与短连接 短连接:客户端与服务端每进行一次报文收发交易时才进行通讯连接.交易完毕后立即断开连接. 长连接:客户端与服务端先建立连接, 连接建立后不断开,然后在进行报文发送和接收 ...
- Consul内部分享ppt
Consul 是一个支持多数据中心,分布式,高可用的服务发现和配置共享系统.由 HashiCorp 公司使用 Go 语言开发,基于Raft协议.部署起来非常容易,只需要极少的可执行程序和配置文件,具有 ...
- Tomcat8源码笔记(二)Bootstrap启动
TOMCAT源码调试入口是Bootstrap类的main方法,我的启动参数VM: -Dcatalina.home=E:/Tomcat_Source_Code/apache-tomcat-8.0.53- ...
- SQL SERVER 如何声明一个变量
DECLARE @i AS INT; ; GO 或者 ; GO 表表达式形式 ); SET @empname = (SELECT firstname + N' ' + lastname FROM HR ...