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
android时间戳字体,Android获取当前时间戳?相关推荐
- android单线字体,Android自定义字体
在main文件夹下,新建assets/fonts文件,添加.otf文件 image.png 字体工具类 import android.app.Application; import android.g ...
- android 变化字体,android 字体修改
android字体的设置有以下方法: 1)直接在代码设置Typeface customFont = Typeface.createFromAsset(this.getAssets(), "1 ...
- python获得当前时间戳_python怎么获取当前时间戳
python获取当前时间戳的方法:1.使用time模块,语法为"time.time()":2.使用datetime模块,语法为"datetime.datetime.now ...
- android 动画字体,Android字体多样式和动画TextDrawable
Android字体多样式和动画TextDrawable 在实际的Android开发中,很多时候,需要用TextView表现和展示view的内容和标题.标签之类.但是Android本身提供的Tex ...
- android 开发 字体,Android开发使用自定义字体的实现方法
释放双眼,带上耳机,听听看~! 1.Android系统默认支持三种字体,分别为:"sans", "serif", "monospace 2.在Andr ...
- 获取当前时间戳 php,php获取当前时间戳
php中如何获得当前时间? php中如何获得当前的时间?用fomat()? 一.使用函式 date() 实现 在编辑器中输入,点击回车就可以得知当前的时间.其中Y是代表4位的年份,H是24小时制,i ...
- js获取php时间戳,JS怎么获取当前时间戳
JS获取当前时间戳,可以通过Date.now()方法来实现,返回自1970年1月1日00:00:00 UTC以来经过的毫秒数也就是当前时间戳. 下面我们就结合具体的代码示例,给大家介绍JS怎么获取当前 ...
- android单线字体,Android字体设置
// 自定义字体 custom = new TextView(this); //xx.ttf located at assets/fonts/ typeface = Typeface.createFr ...
- java生成时间戳类型_Java获取当前时间戳的方法有哪些
Java获取当前时间戳的方法有哪些 时间:2017-08-22 来源:华清远见JAVA学院 时间戳就是一种类型,只是精度很高,比datetime要精确的多,通常用来防止数据出现脏读现象.那么J ...
最新文章
- c++ smart pointer 趣谈
- 从输入网址到网页显示过程
- 16-python基础5-文件读写操作
- Android 清除png图片的白色背景
- 机器学习 预测模型_使用机器学习模型预测心力衰竭的生存时间-第一部分
- MSP430F5529 DriverLib 库函数学习笔记(十五)SFR 模块
- 由旅行商问题认识何为状态压缩
- php 删除指定html标签,php删除html标签的三种解决办法
- 26个Jquery使用小技巧(jQuery tips, tricks amp; solutions)
- buffer string builder简单说明
- 【机器学习-西瓜书】二、偏差-方差分解;泛化误差
- 安大计算机学院李炜教授,李炜-北京航空航天大学计算机学院
- 关于实验室数据结构实验错误本周总结。引用调用bug
- 如何学习PLC编程,有没有什么好的方法?
- 《人是如何学习的》的读后感范文4000字
- poj1386 Paly onWords
- 正版Mincraft登录问题:微软账号不能登录
- c语言两个字母如何比较,c如何比较两个字符 C语言中怎么判断两个字符相同
- 一起自学SLAM算法:11.5 强化学习与自主导航
- 双向dijkstra