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 time

See 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 date

Date 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,布布扣,bubuko.com

时间: 2024-12-18 12:24:07

Get current time and date on Android的相关文章

Android学习七:new Date使用

1.例子 学习时间函数,并实现了简单的多个按钮监听同一个事件的方法 2.代码 代码很简单,也很清晰 package com.example.datetime; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; import android.view.Menu;

Android 更新系统api接口需要同时提交current.txt

修改framework/base下面的api要注意更新api/current.txt文件 更新android系统接口后,只是提交java文件会导致其他人编译不通过,需要提交/framework/base/api下面更新的三个问文件: 如果修改了Android原有API的 ,需要update frameworks/base/api/current.txt.否则编译被中断并出现编译错误提示. 否则,编译时会报以下错误: frameworks/base/services/java/com/androi

Android中使用代码截图的各种方法总结

1,基于Android SDK的截屏方法 (1)主要就是利用SDK提供的View.getDrawingCache()方法.网上已经有很多的实例了.首先创建一个android project,然后进行Layout,画一个按键(res/layout/main.xml): <?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android

getDrawingCache()和Android中的截图方法简介

getDrawingCache()方法截取部分屏幕: view.setDrawingCacheEnabled(true);//设置能否缓存图片信息(drawing cache) view.buildDrawingCache();//如果能够缓存图片,则创建图片缓存 Bitmap bitmap = view.getDrawingCache();//如果图片已经缓存,返回一个bitmap view.destroyDrawingCache();//释放缓存占用的资源 Tips: 如果在一个界面中,重复

Android自定义照相机 预览拍照 切换前后置摄像头

Android提供了Camera来控制拍照,步骤如下:(1)调用Camera的open()方法打开相机.(2)调用Camera的getParameters()获取拍照参数,该方法返回一个Cmera.Parameters对象.(3)调用Camera.Parameters对象对照相的参数进行设置.(4)调用Camera的setParameters(),并将Camera.Parameters对象作为参数传入,这样就可以对拍照进行参数控制,Android2.3.3以后不用设置.(5)调用Camerade

android学习十八(Service服务的基本用法)

定义一个服务 在项目中定义一个服务,新建一个ServiceTest项目,然后在这个项目中新增一个名为MyService的类,并让它继承自Service,完成后的代码如下所示: package com.jack.servicetest; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class MyService extends Service { @Over

HBuilder 获取通话记录 (Android)

Date.prototype.Format = function (fmt) { var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours()%12==0?12:this.getHours()%12, //小时 "H+": this.getHours(), "m+": this.getMin

android 安卓 listview 支持下拉刷新 上拉加载更多

[1]重写listView import java.text.SimpleDateFormat; import java.util.Date; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.ViewGrou

Android Studio 1.5启动出现“SDK Manager: failed to install”问题的解决

问题描述 Android Studio 1.5是当前最新Android手机应用开发平台,下载bundle版安装后,启动Studio后出现“SDK Manager: failed to install”问题.详细信息如下: Android SDK was installed to C:\Users\jack\AppData\Local\Android\sdk Installing Archives:  Preparing to install archives  Installing SDK Pl