[英]I am trying to convert a String date to long
我正在将字符串 date 转换为long
,但我不明白如何返回long
值以及如何将字符串转换为 long 并存储在房间数据库中
package com.example.mybugetssimple;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateConverter {
public static long dateCon(String string_date){
string_date = "12-December-2012";
SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy");
try {
Date d = f.parse(string_date);
long milliseconds = d.getTime();
return milliseconds;
} catch (ParseException e) {
e.printStackTrace();
}
return 1;
}
}
用这种方式
public static long dateCon(String string_date){
Calendar calendar = Calendar.getInstance();
SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy");
Date d = null;
try {
d = f.parse(string_date);
calendar.setTime(d);
} catch (ParseException e) {
e.printStackTrace();
}
return calendar.getTimeInMillis();
}
尝试这个
public static String getStringDateFormTimeStamp(long timestamp) {
try {
Calendar calendar = Calendar.getInstance();
TimeZone tz = TimeZone.getDefault();
calendar.setTimeInMillis(timestamp);
calendar.add(Calendar.MILLISECOND, tz.getOffset(calendar.getTimeInMillis()));
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
Date currenTimeZone = calendar.getTime();
return sdf.format(currenTimeZone);
} catch (Exception e) {
Timber.d(e);
}
return "";
}
public static long StringDateToTimeStamp(String string_date){
Calendar calendar = Calendar.getInstance();
SimpleDateFormat f = new SimpleDateFormat("dd-MM-yyyy");
Date d = null;
try {
d = f.parse(string_date);
calendar.setTime(d);
} catch (ParseException e) {
e.printStackTrace();
}
return calendar.getTimeInMillis();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.