gpt4 book ai didi

Android:将日期转换为毫秒

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:20:18 25 4
gpt4 key购买 nike

我用过

SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
String time = formatter.format(new Date());

为了获取时间 (12.03.2012, 17:31),现在我想将这个时间转换为毫秒,因为我有一个包含几个日期和文本的文件,我想以毫秒为单位转换日期,以便我无法使用

在收件箱中添加文本
ContentValues values = new ContentValues();
values.put("address", "123");
values.put("body", "tekst");
values.put("read", 1);
values.put("date", HERE I MUST PUT A DATE IN MILLISECONDS);
context.getContentResolver().insert(Uri.parse("content://sms/inbox"), values);

因为我必须以毫秒为单位设置时间,所以我必须转换时间,有人知道怎么做吗?

假设我有一个时间 05.01.2011, 12:45 并且想要转换它,如何转换?我想转换我拥有的旧时间(不是从当前时间获取毫秒数)。

最佳答案

最简单的方法是将Date类型转换为毫秒:

SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
formatter.setLenient(false);

Date curDate = new Date();
long curMillis = curDate.getTime();
String curTime = formatter.format(curDate);

String oldTime = "05.01.2011, 12:45";
Date oldDate = formatter.parse(oldTime);
long oldMillis = oldDate.getTime();

关于Android:将日期转换为毫秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9671085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com