Get current time and date on Android
You could use:
Calendar c =Calendar.getInstance();int seconds = c.get(Calendar.SECOND);
There are plenty of constants in Calendar for everything you need. Edit: Calendar class documentation
#########
You can use android.text.format.Time:
Time now =newTime();
now.setToNow();
From the reference linked above:
The Time class is a faster replacement for the java.util.Calendar and java.util.GregorianCalendar classes. An instance of the Time class represents a moment in time, specified with second precision.
If you want to get the date and time in a specific pattern you can use the following:
SimpleDateFormat sdf =newSimpleDateFormat("yyyyMMdd_HHmmss");String currentDateandTime = sdf.format(newDate());Actually, it's safer to set the current timezone set on the device with
Time.getCurrentTimezone(), or else you will get the current time in UTC.Time today =newTime(Time.getCurrentTimezone());
today.setToNow();Then, you can get all the date fields you want, like, for example:
textViewDay.setText(today.monthDay +"");// Day of the month (1-31)
textViewMonth.setText(today.month +"");// Month (0-11)
textViewYear.setText(today.year +"");// Year
textViewTime.setText(today.format("%k:%M:%S"));// Current timeSee android.text.format.Time class for all the details.
To ge the current time you can use
System.currentTimeMillis()which is standard in Java. Then you can use it to create a dateDate currentDate =newDate(System.currentTimeMillis());And as mentioned by others to create a time
Time currentTime =newTime();
currentTime.setToNow();For those who might rather prefer a customized format, you can use:
DateFormat df =newSimpleDateFormat("EEE, d MMM yyyy, HH:mm");String date = df.format(Calendar.getInstance().getTime());Whereas you can have DateFormat patterns such as:
"yyyy.MM.dd G 'at' HH:mm:ss z"----2001.07.04 AD at 12:08:56 PDT
"hh 'o''clock' a, zzzz"-----------12 o'clock PM, Pacific Daylight Time
"EEE, d MMM yyyy HH:mm:ss Z"------- Wed, 4 Jul 2001 12:08:56 -0700
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"------- 2001-07-04T12:08:56.235-0700
"yyMMddHHmmssZ"-------------------- 010704120856-0700
"K:mm a, z" ----------------------- 0:08 PM, PDT
"h:mm a" -------------------------- 12:08 PM
"EEE, MMM d, ''yy" ---------------- Wed, Jul 4, '01 http://stackoverflow.com/questions/5369682/get-current-time-and-date-on-android
Get current time and date on Android的更多相关文章
- Android 使用date set命令修改系统时间
测试环境:android 7.1.1 在adb shell中试图使用 date -s "yyyymmdd.[[[hh]mm]ss]"修改系统系统时间时,会提示 date: Unkn ...
- Android学习七:new Date使用
1.例子 学习时间函数,并实现了简单的多个按钮监听同一个事件的方法 2.代码 代码很简单,也很清晰 package com.example.datetime; import java.text.Sim ...
- Android开发笔记:安卓程序截屏方法
1,基于Android SDK的截屏方法 (1)主要就是利用SDK提供的View.getDrawingCache()方法.网上已经有很多的实例了.首先创建一个android project,然后进行L ...
- Android Camera系列开发 (二)通过Intent录制视频
Android Camera系列开发 (二)通过Intent录制视频 作者:雨水 2013-8-18 CSDN博客:http://blog.csdn.net/gobitan/ 概述 使用Camera ...
- android App Widgets
http://developer.android.com/guide/practices/ui_guidelines/widget_design.html#design http://develope ...
- Android多种方法显示当前日期和时间
文章选自StackOverflow(简称:SOF)精选问答汇总系列文章之一,本系列文章将为读者分享国外最优质的精彩问与答,供读者学习和了解国外最新技术.本文探讨Android显示当前日期和时间的方法. ...
- 【译】Android 数据库 ORMLite
@DatabaseField cloumnName:指定字段名,不指定则变量名作为字段名 canBeNull:是否可以为null dataType:指定字段的类型 defaultValue:指定 ...
- Android随笔之——Android时间、日期相关类和方法
今天要讲的是Android里关于时间.日期相关类和方法.在Android中,跟时间.日期有关的类主要有Time.Calendar.Date三个类.而与日期格式化输出有关的DateFormat和Simp ...
- Android随笔之——闹钟制作铺垫之AlarmManager详解
说实话,之前写的两篇博客Android广播机制Broadcast详解.Android时间.日期相关类和方法以及现在要写的,都算是为之后要写的闹钟应用做铺垫,有兴趣的话,大家可以去看看前两篇博客. 一. ...
随机推荐
- ruby学习--block
#当前块 class Block def a_method return yield if block_given? 'no block' end end obj=Block.new puts &qu ...
- Asp.Net-创建网站的快捷方式到桌面,开始菜单,收藏夹
/// <summary> /// 创建快捷方式 /// </summary> /// <param na ...
- php学习笔记4--php中的变量作用域
变量作用域:可以简单地理解为变量的可见区域,变量能被访问的范围.如同其他语言,php中也有全局作用域和局部作用域之分,但是不同的是:php中的全局作用域指的是:只能在函数外部使用,而局部作用域指的是: ...
- part 3 Controllers in AngularJS
What happens if the controller name is misspelled? When the controller name is misspelled, 2 things ...
- 可以伸缩的查询面板 (searchBar)
最近有这样的需求,一个页面查询条件特别多,一次全部展示出来的话就占用大量的空间,所以分成了两类,简单搜索和高级搜索,当点击高级搜索的时候就会全部显示. 这样就存在一个问题,页面(navTab,dial ...
- asp.net 下载Excel (数据流,不保存)--客户端
效果图: 前端页面 <html> <head> <title>Test For Excel</title> <script src="j ...
- Runtime学习与使用(一):为UITextField添加类目实现被键盘遮住后视图上移,点击空白回收键盘
OC中类目无法直接添加属性,可以通过runtime实现在类目中添加属性. 在学习的过程中,试着为UITextField添加了一个类目,实现了当TextField被键盘遮住时视图上移的功能,顺便也添加了 ...
- 以莫泰的形式进行页面转换(传值用block)
1.在第一个页面进入第二个页面可以使用莫泰的方式 在第一个页面包含第二个页面的头文件#import "FirstViewController.h"#import "Vie ...
- php连接到数据库
html代码: <form action="php_mysql_add.php" method="post"> 用户名: <input typ ...
- Tomcat 优化 java.lang.OutOfMemoryError: Java heap space 的解决方法
Tomcat 优化 java.lang.OutOfMemoryError: Java heap space 的解决方法 java.lang.OutOfMemoryError: Java heap sp ...