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时间.日期相关类和方法以及现在要写的,都算是为之后要写的闹钟应用做铺垫,有兴趣的话,大家可以去看看前两篇博客. 一. ...
随机推荐
- .net C# 网页播放器 支持多种格式 媒体播放器 播放器 代码
.avi格式代码片断如下:<object id='video' width='400' height='200' border='0' classid='clsid:CFCDAA03-8BE4- ...
- HTML页面中常见的一些小方法
在<Head>标签中加 <meta http-equiv="pragma " content="no-cache"> <met ...
- Redis中hash表中的field的value自增可以用hincrby
Redis HINCRBY命令用于增加存储在字段中存储由增量键哈希的数量.如果键不存在,新的key被哈希创建.如果字段不存在,值被设置为0之前进行操作. 回复整数,字段的增值操作后的值. redis ...
- TransmitFile下载文件(部分转载)
例子代码: public void Down() { TransmitFile(@"/File/KBPub.zip"); } public void TransmitFile(st ...
- 使用T4为数据库自动生成实体类
T4 (Text Template Transformation Toolkit) 是一个基于模板的代码生成器.使用T4你可以通过写一些ASP.NET-like模板,来生成C#, T-SQL, XML ...
- How Old Are You?
今天要分享的是一个测年龄的小应用,就类似是http://how-old.net官网测年龄的功能一样,我的也是这样一个功能,细节捕获当然没有how-old多啦,不过这些主要是基于一个第三方的jar包,我 ...
- UI3_UIView自动布局
// // ViewController.m // UI3_UIView自动布局 // // Created by zhangxueming on 15/7/1. // Copyright (c) 2 ...
- Fragstats景观分析研究
QQ 交流群: Fragstats景观分析 加群链接:http://url.cn/N4wZ3N
- 【Newtonsoft.Json】Window Phone Json解析开发包
WP从服务器.API交换数据一般都是用JSON格式字符串. 下面介绍用Newtonsoft.Json来处理JSON. 准备 1.到 http://json.codeplex.com/ 下载Newton ...
- log tag
Media Player MediaPlayerService NuPlayerDecoder NuPlayer MediaFocusControl Ac ...