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 月 ...
随机推荐
- 分布式任务调度系统xxl-job源码探究(一、客户端)
前面讲了xxl-job的搭建,现在来粗略的解析下该分布式调度系统的源码,先来客户点代码 客户端源码 客户端开启的时候会向服务中心进行注册,其实现用的是jetty连接,且每隔半分钟会发送一次心跳,来告诉 ...
- 第三篇: 服务消费者(Feign)
本文根据https://blog.csdn.net/forezp/article/details/81040965写出,修正了部分瑕疵,在此对那位博主表示感谢. 上一篇文章讲述通过RestTempla ...
- HttpClient和HttpURLConnection的使用和区别(下)
转自来自点击打开链接 接着上一篇,我们继续来分析HttpURLConnection的使用,以及两者的共同点和区别. 目录 用法 HttpURLConnection 区别 引用资料 用法 HttpURL ...
- 微信开发之获取openid及推送模板消息
有很多的朋友再问我怎么获取code,openid之类的问题,在这里我就给大家分享一下. 在做微信支付是需要获取openid的,推送模板消息也是需要openid包括其他一些功能分享等也都是需要的,ope ...
- [视频]K8飞刀 shellcode loader演示教程
[视频]K8飞刀 shellcode loader演示教程 https://pan.baidu.com/s/1eQ77lPw
- Xamarin.Android 使用SoundPool进行音频播放
一.引入命名空间 using Android.Media; 二.声明变量 SoundPool soundPool; int soundPoolId; 三.实例化对象 soundPool = new S ...
- WebSocket connection to 'ws://xx:9502/' failed:Error in connection establishment:net::ERR_CONNECTION_TIMED_OUT
1.首先看能否ping通服务器 2.telnet xx 9502之后能不能连通 3.如果连不通有可能是防火墙的问题 可以试试清空防火墙规则,或者关闭防火墙 iptables -F
- Excel设置excel打印每页都有表头标题
Excel设置excel打印每页都有表头标题
- 从零开始学 Web 之 移动Web(一)屏幕相关基本知识,调试,视口,屏幕适配
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...
- 详解 Java NIO
文件的抽象化表示,字节流以及字符流的文件操作等属于传统 IO 的相关内容,我们已经在前面的文章进行了较为深刻的学习了. 但是传统的 IO 流还是有很多缺陷的,尤其它的阻塞性加上磁盘读写本来就慢,会导致 ...