我有一个日期和时间格式如下:
Tue Apr 23 16:08:28 GMT+05:30 2013
我想转换成毫秒,但实际上我不知道它是哪种格式。任何人都可以帮助我。
答案 0 :(得分:75)
使用以下方法
String givenDateString = "Tue Apr 23 16:08:28 GMT+05:30 2013";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
try {
Date mDate = sdf.parse(givenDateString);
long timeInMilliseconds = mDate.getTime();
System.out.println("Date in milli :: " + timeInMilliseconds);
} catch (ParseException e) {
e.printStackTrace();
}
答案 1 :(得分:4)
您可以使用此代码
long miliSecsDate = milliseconds ("2015-06-04");
Log.d("miliSecsDate", " = "+miliSecsDate);
public long milliseconds(String date)
{
//String date_ = date;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try
{
Date mDate = sdf.parse(date);
long timeInMilliseconds = mDate.getTime();
System.out.println("Date in milli :: " + timeInMilliseconds);
return timeInMilliseconds;
}
catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
答案 2 :(得分:2)
Date beginupd = new Date(cursor1.getLong(1));
此变量beginupd包含格式
Wed Oct 12 11:55:03 GMT+05:30 2011
long millisecond = beginupd.getTime();
Date.getTime() JavaDoc states:
返回自此Date对象表示的1970年1月1日00:00:00 GMT以来的毫秒数。
答案 3 :(得分:2)
将日期和时间字符串转换为毫秒:
public static final String DATE_TIME_FORMAT = "MM/dd/yyyy HH:mm:ss a";
或
public static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
或
public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZZZZZ";
//TimeZone.getAvailableIds() to list all timezone ids
String timeZone = "EST5EDT";//it can be anything timezone like IST, GMT.
String time = "2/21/2018 7:41:00 AM";
public static long[] convertTimeInMillis(String dateTimeFormat, String timeZone, String... times) throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat(dateTimeFormat, Locale.getDefault());
dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
long[] ret = new long[times.length];
for (int i = 0; i < times.length; i++) {
String timeWithTZ = times[i] + " "+timeZone;
Date d = dateFormat.parse(timeWithTZ);
ret[i] = d.getTime();
}
return ret;
}
// millis to dateString
public static String convertTimeInMillisToDateString(long timeInMillis, String DATE_TIME_FORMAT) {
Date d = new Date(timeInMillis);
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
return sdf.format(d);
}
答案 4 :(得分:2)
在Kotlin,
只需使用
timeInMilSeconds = date.time
其中timeInMilSeconds
是毫秒(var timeInMilSeconds: Long
)而date
是Date
答案 5 :(得分:1)
在科特林尝试一下,
val calendar = Calendar.getInstance()
val UniqueID = calendar.timeInMillis
Java中的相同代码
Calendar calendar = Calendar.getInstance(); long time = calendar.getTimeInMillis();
答案 6 :(得分:1)
我提供了现代的答案-尽管还不及它在您的Android API级别上可以使用的现代程度。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"EEE MMM dd HH:mm:ss OOOO yyyy", Locale.ROOT);
String givenDateString = "Tue Apr 23 16:08:28 GMT+05:30 2013";
long timeInMilliseconds = OffsetDateTime.parse(givenDateString, formatter)
.toInstant()
.toEpochMilli();
System.out.println(timeInMilliseconds);
此代码段的输出为:
1366713508000
大多数其他答案中使用的SimpleDateFormat
,Date
和Calendar
类的设计很差,现在已经过时了。我建议您改用java.time,这是现代的Java日期和时间API。编辑:2013年提出该问题时,其他答案是很好的答案。只有时间在流逝,我们不应该再使用SimpleDateFormat
,Date
和Calendar
。
java.time在较新和较旧的Android设备上均可正常运行。它只需要至少 Java 6 。
org.threeten.bp
导入日期和时间类。java.time
。java.time
向Java 6和7(JSR-310的ThreeTen)的反向端口。答案 7 :(得分:0)
答案 8 :(得分:0)
Android 26及更高版本
Conversation from epoch to UTC date time and Device date time
public class TimeConversionUtil {
public static long getCurrentEpochUTC() {
return Instant.now(Clock.systemUTC()).toEpochMilli();
}
public static String deviceDateTimeString(long epochMilliUtc) {
Instant instant = Instant.ofEpochMilli(epochMilliUtc);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd, yyyy, hh:mm:ss a", Locale.US).withZone(ZoneId.systemDefault());
return formatter.format(instant);
}
public static String uTCDateTimeString(long epochMilliUtc) {
Instant instant = Instant.ofEpochMilli(epochMilliUtc);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd, yyyy, hh:mm:ss a", Locale.US).withZone(ZoneId.of("UTC"));
return formatter.format(instant);
}
public static long convertDateStringToLongUTC(String stringUTCDate) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd, yyyy, hh:mm:ss a", Locale.ENGLISH);
LocalDateTime localDate = LocalDateTime.parse(stringUTCDate, formatter);
long timeInMilliseconds = localDate.atOffset(ZoneOffset.UTC).toInstant().toEpochMilli();
return timeInMilliseconds;
}
}
@Test
public void timeConversionTest() {
long currentTimeUtc = Instant.now(Clock.systemUTC()).toEpochMilli();
String utc = TimeConversionUtil.uTCDateTimeString(currentTimeUtc);
Long utcLongTime = TimeConversionUtil.convertDateStringToLongUTC(utc);
String utc1 = TimeConversionUtil.uTCDateTimeString(utcLongTime);
assertTrue(utc.equalsIgnoreCase(utc1));
}