android 时间获取以及时间格式化
Android中获取系统时间有多种方法,可分为Java中Calendar类获取,java.util.date类实现,还有android中Time实现
现总结如下:
方法一;
- void getTime1(){
- long time=System.currentTimeMillis();//long now = android.os.SystemClock.uptimeMillis();
- SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date d1=new Date(time);
- String t1=format.format(d1);
- Log.e("msg", t1);
- }
方法二;
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
- String t=format.format(new Date());
- Log.e("msg", t);
方法三;
- void getTime3(){
- Calendar calendar = Calendar.getInstance();
- String created = calendar.get(Calendar.YEAR) + "年"
- + (calendar.get(Calendar.MONTH)+1) + "月"//从0计算
- + calendar.get(Calendar.DAY_OF_MONTH) + "日"
- + calendar.get(Calendar.HOUR_OF_DAY) + "时"
- + calendar.get(Calendar.MINUTE) + "分"+calendar.get(Calendar.SECOND)+"s";
- Log.e("msg", created);
- }
方法四;
- void getTime4(){
- Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
- t.setToNow(); // 取得系统时间。
- String time=t.year+"年 "+(t.month+1)+"月 "+t.monthDay+"日 "+t.hour+"h "+t.minute+"m "+t.second;
- Log.e("msg", time);
- }
获取星期日期:
- Calendar calendar = Calendar.getInstance();
- int day = calendar.get(Calendar.DAY_OF_WEEK);
- String today = null;
- if (day == 2) {
- today = "Monday";
- } else if (day == 3) {
- today = "Tuesday";
- } else if (day == 4) {
- today = "Wednesday";
- } else if (day == 5) {
- today = "Thursday";
- } else if (day == 6) {
- today = "Friday";
- } else if (day == 7) {
- today = "Saturday";
- } else if (day == 1) {
- today = "Sunday";
- }
- System.out.println("Today is:- " + today);
最后说一下日期格式化,日期格式化通常使用SimpleDateFormat类实现,其中的日期格式不能够自己随意定义,主要有以下几种形式:
SimpleDateFormat f1= new SimpleDateFormat(); //其中没有些格式化参数,我们使用默认的日期格式。
System.out.println(f.formate(new Date()));
代码输出的日期格式为:12-3-22 下午4:36
SimpleDateFormat f4= new SimpleDateFormat("今天是"+"yyyy年MM月dd日 E kk点mm分");//可根据不同样式请求显示不同日期格式,要显示星期可以添加E参数
System.out.println(f4.format(new Date()));
代码输出的日期格式为:今天是2012年03月22日 星期四 16点46分
SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
System.out.println("Date to String "+formater.format(new Date()));
相近的常用形式还有 yyMMdd hh:mm:ss yyyy-MM-dd hh:mm:ss dd-MM-yyyy hh:mm:ss
应有的时候通常还会需要把具体日期转换为毫秒或者Timestamp形式,如下:
文本 - > Timestamp,日期 -> Timestamp
Timestamp t ;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try ...{
t = new Timestamp(format.parse("2007-07-19 00:00:00").getTime());
} catch (ParseException e) ...{
e.printStackTrace();
}
Timestamp t ;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
t = new Timestamp(new Date().getTime());
在做读取短信的时候,读取到date字段,需要转换成例如7/21 或者 8:21这种格式,不用手动转换 通过调用相应的类来实现
SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");// 这里我们可以指定可是 例如 MM-DD就是只显示月和日 。。。。
Date date = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex("date"))));//从短信中获得时间
String time = sfd.format(date); //转换完成的字符串 很简单
dataTextView.setText(time); //显示出来就行了
原文链接:http://blog.csdn.net/yudajun/article/details/7939552
android 时间获取以及时间格式化的更多相关文章
- android java获取当前时间的总结
import java.text.SimpleDateFormat; SimpleDateFormat formatter = new SimpleDateFormat (&q ...
- SQL传入时间获取到时间的周一和周日
declare @time datetime declare @timeMonday datetime set @time='2013-11-07' ) ,@time) select @timeMon ...
- Python中获取当前时间 获取当前时间前几天的代码
当然 需要引入 datetime import datetime 获取当前日期:datetime.datetime.now().strftime('%Y-%m-%d') 获取当前日期前七天日期: no ...
- Android获取系统时间的多种方法
Android中获取系统时间有多种方法,可分为Java中Calendar类获取,java.util.date类实现,还有android中Time实现. 现总结如下: 方法一: ? 1 2 3 4 5 ...
- js获取当前时间&js 页面时钟
js获取当前时间 //获取当前时间,格式YYYY-MM-DD function getNowFormatDate() { var date = new Date(); var seperator1 = ...
- Shell获取指定时间
时区基本知识 相差一个时区(经度15度)时间相差一小时.简单计算口诀:1.同一个时区的相差时间用减法,2.不同时区的相差时间用加法.3东加西减.例如一,东八区是8点,问东1区几点.根据上面口诀1,算: ...
- 008——转载——c#获取当前日期时间
(一)转载——c#获取当前日期时间 我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04).时间(12:12:12).日 ...
- iOS 获取当前时间格式化字符串
iOS 获取当前时间格式化字符串 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保 ...
- Android获取系统时间方法的总结
Android获取系统时间方法的方法有很多种,常用的有Calendar.Date.currentTimeMills等方法. (1)Calendar Calendar获取系统时间首先要用Calendar ...
随机推荐
- SDP(11):MongoDB-Engine功能实现
根据上篇关于MongoDB-Engine的功能设计方案,我们将在这篇讨论里进行功能实现和测试.下面是具体的功能实现代码:基本上是直接调用Mongo-scala的对应函数,需要注意的是java类型和sc ...
- 【Luogu3806】点分治(点分治)
[Luogu3806]点分治(点分治) 题面 题目描述 给定一棵有n个点的树 询问树上距离为k的点对是否存在. 输入格式: n,m 接下来n-1条边a,b,c描述a到b有一条长度为c的路径 接下来m行 ...
- 读取MySQL存储二进制的语音、图片(Blob类型)
/** * 下载语音 * Remarks: * @throws Exception */ public void downloadYuyin() throws Exception { ...
- Django 2.0 学习(02):Django视图和URL(上)
接上篇博文,接下来我们以具体代码例子来说明Django的基本流程. 创建项目 使用Win+R,输入cmd进图windows命令行模式: 再你想要存放项目工作的磁盘,输入下面命令: django-adm ...
- Linux shell 脚本(二)
转载请标明出处: http://blog.csdn.net/zwto1/article/details/45078837: 本文出自:[明月的博客] 五.字符串处理 1.子串截取操作: 路径分割: ...
- OOAD与UML
UML基础介绍 1.UML的定义 统一建模语言(UML)是一种图形化的语言,它可以帮助我们在OOAD过程中标识元素.构建模块.分析过程并可通过文档说明系统中的重要细节 2.OOAD OOAD是根据OO ...
- libpqxx接口的在linux下的使用,解决psql:connections on Unix domain socket "/tmp/.s.PGSQL.5432"错误
在项目中使用postgresql数据库时要求在windows和linux双平台兼容.于是在windows下使用的接口在linux下爆出异常: psql:connections on Unix doma ...
- 初学Python3 - 写一个登录程序
本篇主要实现一个简单的登录程序,默认给出一个账号密码,贴出写的代码及过程中遇到的问题. ----------------------------------------要求如下: username p ...
- The POM for ... is missing, no dependency information available
今天在重温淘淘商城的项目,准备用idea重写次,换个bootstrap的前端框架,但是在用idea构建maven项目后编译时却报错了: 经再三确认,common工程自身并没有任何问题,引用这个工程的地 ...
- PHP中文关键词匹配
关键词匹配是比较常见的需求,如留言.弹幕及游戏聊天中的敏感词过滤,都需要对一段文字进行关键词匹配.提取到关键词后,再做进一步处理. 本类借助PHP高效的数组和mbstring扩展,来实现对中文关键词的 ...