将字符串转换为getTimeInMillis()

问题描述 投票:0回答:1

如何将“ dateString”转换为getTimeInMillis()?我想将getTimeInMillis()值放入AlarmManager。

SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String dateString = dateformatter.format(new Date(now.getTimeInMillis()));
java android alarmmanager simpledateformat date-format
1个回答
0
投票
import java.text.ParseException; import java.text.SimpleDateFormat; class Main { public static void main(String[] args) { SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy-MM-dd HH:mm"); long ms; try { ms = dateformatter.parse("2020-02-17 21:30").getTime(); System.out.println(ms); } catch (ParseException e) { e.printStackTrace(); } } }

输出:1581975000000

注意:以Ole V.V.已经指出,您应该切换到现代Date/ Time API

© www.soinside.com 2019 - 2024. All rights reserved.