将日期格式转换为毫秒
要以 dd / MM / yyyy 格式将日期转换为毫秒,可以使用数据作为字符串调用此函数
public long getMilliFromDate(String dateFormat) {
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
try {
date = formatter.parse(dateFormat);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("Today is " + date);
return date.getTime();
}
此方法将毫秒转换为时间戳格式日期:
public String getTimeStamp(long timeinMillies) {
String date = null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // modify format
date = formatter.format(new Date(timeinMillies));
System.out.println("Today is " + date);
return date;
}
此方法将特定的日,月和年转换为毫秒。使用 Timpicker
或 Datepicker
时会非常有帮助
public static long getTimeInMillis(int day, int month, int year) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
return calendar.getTimeInMillis();
}
它将从日期返回毫秒
public static String getNormalDate(long timeInMillies) {
String date = null;
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
date = formatter.format(timeInMillies);
System.out.println("Today is " + date);
return date;
}
它将返回当前日期
public static String getCurrentDate() {
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String formattedDate = df.format(c.getTime());
return formattedDate;
}
注意: Java 提供日期格式支持日期模式的数量