android时间戳字体,Android获取当前时间戳?

Android获取当前时间戳?

我想得到当前的时间戳:1320917972

int time = (int) (System.currentTimeMillis());

Timestamp tsTemp = new Timestamp(time);

String ts = tsTemp.toString();

11个解决方案

236 votes

解决方案是:

Long tsLong = System.currentTimeMillis()/1000;

String ts = tsLong.toString();

Rjaibi Mejdi answered 2019-03-09T00:34:38Z

74 votes

来自开发者博客:

System.currentTimeMillis()是标准的“墙”时钟(时间和日期),表示自纪元以来的毫秒数。 挂钟可以由用户或电话网络设置(请参阅setCurrentTimeMillis(long)),因此时间可能会不可预测地向后或向前跳转。 只有在与实际日期和时间对应很重要时,例如在日历或闹钟应用程序中,才应使用此时钟。 间隔或经过时间测量应使用不同的时钟。 如果您使用的是System.currentTimeMillis(),请考虑收听ACTION_TIME_TICK,ACTION_TIME_CHANGED和ACTION_TIMEZONE_CHANGED Intent广播,以了解时间的变化。

drooooooid answered 2019-03-09T00:35:14Z

27 votes

1320917972是Unix时间戳,使用自1970年1月1日00:00:00 UTC以来的秒数。您可以使用TimeUnit类进行单位转换 - 从System.currentTimeMillis()到秒。

String timeStamp = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));

sealskej answered 2019-03-09T00:35:45Z

22 votes

您可以使用SimpleDateFormat类:

SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");

String format = s.format(new Date());

Pratik Butani answered 2019-03-09T00:36:16Z

20 votes

使用以下方法获取当前时间戳。 这对我来说可以。

/**

*

* @return yyyy-MM-dd HH:mm:ss formate date as string

*/

public static String getCurrentTimeStamp(){

try {

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String currentDateTime = dateFormat.format(new Date()); // Find todays date

return currentDateTime;

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

Hits answered 2019-03-09T00:36:47Z

9 votes

它很简单:

long millis = new Date().getTime();

如果你想要特定的格式,那么你需要像下面的格式化程序

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String millisInString = dateFormat.format(new Date());

Pranav answered 2019-03-09T00:37:25Z

8 votes

这是一个可以在文件名中使用的人类可读时间戳,以防万一有人需要我需要的东西:

package com.example.xyz;

import android.text.format.Time;

/**

* Clock utility.

*/

public class Clock {

/**

* Get current time in human-readable form.

* @return current time as a string.

*/

public static String getNow() {

Time now = new Time();

now.setToNow();

String sTime = now.format("%Y_%m_%d %T");

return sTime;

}

/**

* Get current time in human-readable form without spaces and special characters.

* The returned value may be used to compose a file name.

* @return current time as a string.

*/

public static String getTimeStamp() {

Time now = new Time();

now.setToNow();

String sTime = now.format("%Y_%m_%d_%H_%M_%S");

return sTime;

}

}

18446744073709551615 answered 2019-03-09T00:37:52Z

1 votes

java.time

我想提出现代的答案。

String ts = String.valueOf(Instant.now().getEpochSecond());

System.out.println(ts);

刚刚运行时的输出:

1543320466

虽然划分1000对许多人来说并不会让人感到惊讶,但是自己的时间转换很难快速阅读,所以当你可以避免它时,这是一个坏习惯。

我使用的java.time类是java.time的一部分,java.time是现代Java日期和时间API。 它内置于新的Android版本,API级别26及更高版本。 如果你是为较旧的Android编程,你可能会得到后端,见下文。 如果您不想这样做,可以理解,我仍然使用内置转换:

String ts = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));

System.out.println(ts);

这与sealskej的答案相同。 输出与以前相同。

问题:我可以在Android上使用java.time吗?

是的,java.time适用于较旧和较新的Android设备。 它至少需要Java 6。

在Java 8及更高版本和更新的Android设备上(来自API级别26),现代API内置。

在Java 6和7中获取ThreeTen Backport,新类的后端端口(适用于JSR 310的ThreeTen;请参见底部的链接)。

在(较旧的)Android上使用Android版的ThreeTen Backport。 它被称为ThreeTenABP。 并确保从子包中导入java.time的日期和时间类。

链接

Oracle教程:日期时间解释如何使用java.time。

Java规范请求(JSR)310,其中首先描述了java.time。

ThreeTen Backport项目,java.time的后端到Java 6和7(ThreeTen for JSR-310)。

ThreeTenABP,Android版ThreeTen Backport

问题:如何在Android项目中使用ThreeTenABP,有一个非常详尽的解释。

Ole V.V. answered 2019-03-09T00:40:37Z

0 votes

我建议使用Hits的答案,但添加Locale格式,这就是Android的方法  开发人员建议:

try {

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

return dateFormat.format(new Date()); // Find todays date

} catch (Exception e) {

e.printStackTrace();

return null;

}

Faustino Gagneten answered 2019-03-09T00:41:02Z

0 votes

只是用

long millis = new Date().getTime();

以长毫米获得当前时间

Bilal Ahmad answered 2019-03-09T00:41:31Z

0 votes

试试这些

time.setText(String.valueOf(System.currentTimeMillis()));

和timeStamp到时间格式

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

String dateString = formatter.format(new Date(Long.parseLong(time.getText().toString())));

time.setText(dateString);

Mujahid khan answered 2019-03-09T00:42:06Z

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值