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时间.日期相关类和方法以及现在要写的,都算是为之后要写的闹钟应用做铺垫,有兴趣的话,大家可以去看看前两篇博客. 一. ...
随机推荐
- curl模拟浏览器进行phpQuery抓取数据
报Warning: file_get_contents(http://www.dianping.com/shop/8042874) [function.file-get-contents]: fail ...
- 模仿Word中组织结构图的特点生成流程图
//基本原理: // 1. 从下往上计算位置 // 2. 模仿Word中组织结构图的特点 //调用代码: Tree<string> tree = new Tree<string> ...
- DOS批处理命令-for语句
for是为了循环执行一系列命令而执行的命令语句. for要处理的内容不同,语法结构稍有不同.下面就各种情形来分别 1.基本的语法:FOR %変数 IN (set) DO 命令 [参数] 语法内容解析: ...
- js 日期函数用法总结
1 创建Date对象,用于处理日期和时间 var date=new Date(); Date对象会把当前日期和时间保存为初始值. 还可以设置其它参数初始化 Date对象: new Date(" ...
- 理解C#系列 / C#语言的特性
C#语言的特性 大多数语句都已(;)结尾 用({})定义语句块 单行注释(//),多行注释(/*......*/)智能注释(///) 区分大小写 用namespace名称空间对类进行分类 在C#中的所 ...
- UI1_UICollectionView
// AppDelegate.m // UI1_UICollectionView // // Created by zhangxueming on 15/7/16. // Copyright (c) ...
- 使用Linux系统中的SSH服务
使用Linux系统中的SSH服务 1.SSH服务应用场景 ① 可以实现对文件的上传与下载 ② 实现远程管理Linux 2.安装SSH服务器 服 务:sshd 位 置:光盘2 软 件:openssh-s ...
- 《锋利的jQuery》心得笔记--Three Sections
第六章 1. JavaScript的Ajax的实现步骤: 1) 定义一个函数用来异步获取信息 function Ajax(){ } 2) 声明: var xmlH ...
- 10+ powerful debugging tricks with Visual Studio
10+ powerful debugging tricks with Visual Studio Original link : http://www.codeproject.com/Articles ...
- nginx配置多个网址
实战Nginx与PHP(FastCGI)的安装.配置与优化:http://ixdba.blog.51cto.com/2895551/806622 Nginx配置文件详细说明:http://www.cn ...