package com.lyh.face.tools;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class TimeUtil {
private static final String FORMAT_Y_M_D_H_M_S = "yyyy-MM-dd HH:mm:ss";
private static SimpleDateFormat sdf = null;
private static Calendar calendar = null;
/**
* 将字符串转化为Date
*
* @param str
* 传入字符串时间
* @return
*/
public static Date getStringToDate(String str) {
sdf = new SimpleDateFormat(FORMAT_Y_M_D_H_M_S, Locale.getDefault());
try {
return sdf.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取年
* */
public static int getYear(Date date) {
calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.YEAR);
}
/**
* 获取月
* */
public static int getMon(Date date){
calendar = Calendar.getInstance();
calendar.setTime(date);
return (calendar.get(Calendar.MONTH)+1);
}
/**
* 获取日期天
* */
public static int getDay(Date date){
calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DATE);
}
/**
* 获取时间字符串
* */
public static String getTime(String date){
return date.substring(11, (date.length()-3));
}
/**
* 获取年月字符串
* */
public static String getYear_Mon(String date){
return date.substring(0, 7);
}
/**
* 获取当前时间
* */
public static String getNowDateStr() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
String nowdate = df.format(new Date());// new Date()为获取当前系统时间
return nowdate;
}
/**
* 获取当前时间标识码精确到毫秒
* */
public static String getNowtimeKeyStr(){
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");// 设置日期格式
String nowdate = df.format(new Date());// new Date()为获取当前系统时间
return nowdate;
}
/**
* 获取当前时间标识码精确到秒
* */
public static String getNowDateKeyStr(){
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
String nowdate = df.format(new Date());// new Date()为获取当前系统时间
return nowdate;
}
}
Android 当前时间的获取
最新推荐文章于 2024-04-29 17:18:01 发布