Android日期操作
第一种方法
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
//获取当前时间
Date date = new Date(System.currentTimeMillis());
time1.setText("Date获取当前日期时间"+simpleDateFormat.format(date));
第二种方法Calendar calendar = Calendar.getInstance();
//年
int year = calendar.get(Calendar.YEAR);
//月
int month = calendar.get(Calendar.MONTH)+1;
//日
int day = calendar.get(Calendar.DAY_OF_MONTH);
//获取系统时间
//小时
int hour = calendar.get(Calendar.HOUR_OF_DAY);
//分钟
int minute = calendar.get(Calendar.MINUTE);
//秒
int second = calendar.get(Calendar.SECOND);
time2.setText("Calendar获取当前日期"+year+"年"+month+"月"+day+"日"+hour+":"+minute+":"+second);
第三种方法Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
Time time = new Time();
time.setToNow();// 设置当前时间
int year = t.year;
int month = t.month+1;
int day = t.monthDay;
int hour = t.hour; // 0-23
int minute = t.minute;
int second = t.second;
time3.setText("Calendar获取当前日期"+year+"年"+month+"月"+day+"日"+hour+":"+minute+":"+second);
**细心的可能发现月份要加上1,这个是因为是0开始计算的,所以用时要加上1。第一次接触Android的可能最后一个看不懂,time1,time2 ,time3是TextView的对象记得用时需要初始化下
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class KXX {
public static void main(String[] args) {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
Date today = new Date();
System.out.println("今天是:" + f.format(today));
Calendar c = Calendar.getInstance();
c.setTime(today);
c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天
Date tomorrow = c.getTime();
System.out.println("明天是:" + f.format(tomorrow));
}
}
/*
* 将时间转换为时间戳
*/
public static long dateToStamp(String s) throws ParseException {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(s);
long ts = date.getTime();
res = String.valueOf(ts);
return ts;
}
/*
* 将时间戳转换为时间
*/
public static String stampToDate(String s){
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long lt = new Long(s);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
return res;
}
/**
* long转换时分秒
* @param l
* @return
*/
public static String formatLongToTimeStr(Long l) {
int hour = 0;
int minute = 0;
int second = 0;
second = l.intValue() ;
if (second > 60) {
minute = second / 60; //取整
second = second % 60; //取余
}
if (minute > 60) {
hour = minute / 60;
minute = minute % 60;
}
String strTime = hour+":"+minute+":"+second;
return strTime;
}
身份证取出生日期
public static Map<String, String> getBirAgeSex(String certificateNo) {
String birthday = "";
String age = "";
String sexCode = "";
int year = Calendar.getInstance().get(Calendar.YEAR);
char[] number = certificateNo.toCharArray();
boolean flag = true;
if (number.length == 15) {
for (int x = 0; x < number.length; x++) {
if (!flag) return new HashMap<String, String>();
flag = Character.isDigit(number[x]);
}
} else if (number.length == 18) {
for (int x = 0; x < number.length - 1; x++) {
if (!flag) return new HashMap<String, String>();
flag = Character.isDigit(number[x]);
}
}
if (flag && certificateNo.length() == 15) {
birthday = "19" + certificateNo.substring(6, 8) + "-"
+ certificateNo.substring(8, 10) + "-"
+ certificateNo.substring(10, 12);
sexCode = Integer.parseInt(certificateNo.substring(certificateNo.length() - 3, certificateNo.length())) % 2 == 0 ? "F" : "M";
age = (year - Integer.parseInt("19" + certificateNo.substring(6, 8))) + "";
} else if (flag && certificateNo.length() == 18) {
birthday = certificateNo.substring(6, 10) + "-"
+ certificateNo.substring(10, 12) + "-"
+ certificateNo.substring(12, 14);
sexCode = Integer.parseInt(certificateNo.substring(certificateNo.length() - 4, certificateNo.length() - 1)) % 2 == 0 ? "F" : "M";
age = (year - Integer.parseInt(certificateNo.substring(6, 10))) + "";
}
Map<String, String> map = new HashMap<String, String>();
map.put("birthday", birthday);
map.put("age", age);
map.put("sexCode", sexCode);
return map;
}
Android日期操作的更多相关文章
- [Android Pro] 完美Android Cursor使用例子(Android数据库操作)
reference to : http://www.ablanxue.com/prone_10575_1.html 完美 Android Cursor使用例子(Android数据库操作),Androi ...
- java 字符串操作和日期操作
一.字符串操作 创建字符串 String s2 = new String("Hello World"); String s1 = "Hello World"; ...
- JAVASE02-Unit03: 日期操作 、 集合框架
Unit03: 日期操作 . 集合框架 java.util.Date package day03; import java.util.Date; /** * java.util.Date * Date ...
- Android 常用操作
0.android studios使用介绍 使用介绍 android studio 常用小技巧 网址 1.怎么样添加第三方库 方法一: 第一步:将第三方库以module的形式导入 第二步:选中要导入第 ...
- Lua库之时间和日期操作
Lua库之时间和日期操作 (2010-02-07 18:41:20) 转载▼ os.time() <== 返回当前系统的日历时间os.date() <== 返回本地化的时间字符串,这里是& ...
- Android – 学习操作NFC – 2
在<Android – 学习操作NFC – 1>说明了Android在处理NFC tag的机制.tag dispatch system的运作流程,以及三种ACTION_NDEF_DISCO ...
- java日期操作大全
摘自(http://www.blogjava.net/i369/articles/83483.html) java日期操作 大全 先来一个: 取得指定月份的第一天与取得指定月份的最后一天 http ...
- oracle日期操作
日期操作:ADD_MONTHS(date,i) 作用 返回在自定日期上添加的月份 i是整数 如果i是小数,则截取整数部分 i是负数 原有日期减去相应部分 例子: SQL> select add_ ...
- NSDate 总结日期操作
IOS Object-c NSDate总结日期操作 //NSDate //1, 创建NSDate对象 NSDate *nowDate = [NSDate date]; NSLog(@"%@& ...
随机推荐
- jquey动画效果
jquery的事件没有on,js的有. 1.show() 显示 由小变大缓慢显示 <html lang="en"> <head> <meta ch ...
- 隐藏按钮button
<td> <input id="del" type="button" value="删除" onclick="u ...
- 剑指Offer(二十四):二叉树中和为某一值的路径
剑指Offer(二十四):二叉树中和为某一值的路径 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...
- 0029redis单机版环境搭建
linux环境下安装单机版redis,主要分为如下几步: 1. 安装gcc 2.下载安装包 3.解压安装包 4.进入解压目录并执行make和make install命令 5.查看默认安装目录 6.更改 ...
- destoon模板存放调取规则
一.模板存放及调用规则 模板存放于系统 template 目录,template 目录下的一个目录例如 template/default/ 即为一套模板 模板文件以 .htm 为扩展名,可直 ...
- 织梦xss通杀所有版本漏洞【学习笔记】
漏洞原因:DEDECMS由于编辑器过滤不严,将导致恶意脚本运行.可getshell取得权限.为什么说它是0Day呢?能getshell的都算0Day(鸡肋发挥起来也能变凤凰)目前只是测试过5.3到5. ...
- 虚拟机安装Linux从零到登陆成功教学
1.Linux Linux使我们出来windows以外可能接触最多的操作系统了,因为好多超级大的互联网公司,比如阿里等就是用Linux的,所以我们最起码要知道怎样去使用,使用的前提就是我们要有一个这样 ...
- ASCII、Unicode、UTF-8字符集编码
ASCII码 计算机内部,所有信息都是由二进制的字符串表示 每一个二进制位有“0”.“1”两种状态,因此8个二进制位可以表示256个状态,每个状态代表一个符号就是256个符号,从0000000到111 ...
- oracle中日期相关的区间
and czrqb.lsrqb_rh_sj >= to_date('[?query_date_begin|2011-09-01?]A', 'yyyy-mm-dd') and czrqb.lsrq ...
- myeclipse常用快捷键和小技巧
常用快捷键: Ctrl + Shift + R 在整个项目中查找文件 Ctrl + H 查找文件,可以限定文件中包含的内容 Ctrl + Shift + G 查找一个方法在哪里被调用 Ctrl + O ...