import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
// 时间戳转换
public class DateUtils {
private static SimpleDateFormat sf = null;
/*获取系统时间 格式为:"yyyy/MM/dd "*/
public static String getCurrentDate() {
Date d = new Date();
sf = new SimpleDateFormat("yyyyMMddHH");
return sf.format(d);
}
/*获取系统时间 格式为:"yyyy-MM-dd HH:mm:ss"*/
public static String getCurrentDateF() {
Date d = new Date();
sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sf.format(d);
}
/*将字符串转为时间戳*/
public static long getStringToDate(String time) {
sf = new SimpleDateFormat("yyyyMMddHH");
Date date = new Date();
try {
date = sf.parse(time);
} catch (Exception e) {
e.printStackTrace();
}
return date.getTime();
}
/*时间戳转换成字符窜*/
public static String getDateToString(long time) {
Date d = new Date(time);
sf = new SimpleDateFormat("yyyyMMddHH");
return sf.format(d);
}
/**
* 将20150203100255字符串转换为时间 yyyy-MM-dd HH:mm:ss
*
* @param strDate
* @return
*/
public static String strToDate(String strDate) {
SimpleDateFormat sf1 = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat sf2 =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sfstr = "";
try {
sfstr = sf2.format(sf1.parse(strDate));
} catch (ParseException e) {
e.printStackTrace();
}
return sfstr;
}
//将time : 1447830272000转化为yyyy-MM-dd
public static String longToDateStr(long strDate) {
Date date=new Date(strDate);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strs=sdf.format(date);
return strs;
}
//把字符串转为日期
public static Date ConverToDate(String strDate) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = null;
try {
d = df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}
//把日期转换为星期
public static String dateToWeek(Date date) {
SimpleDateFormat sf = new SimpleDateFormat("E");
return sf.format(date);
}
//把日期转换为星期
public static String dateToDate(Date date) {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
return sf.format(date);
}
//获得最近6个年月的集合
public static String[] getLast6Months(){
String[] last6Months = new String[6];
Calendar cal = Calendar.getInstance();
SimpleDateFormat matter=new SimpleDateFormat("yyyy-MM");
cal.add(Calendar.MONTH,+1);
for(int i=0; i<6; i++){
cal.add(Calendar.MONTH, -1);
last6Months[5-i] = matter.format(cal.getTime());
}
return last6Months;
}
//获取当前时间的毫秒之
public static long getDateTimeValues(){
long curren = System.currentTimeMillis();
long currenM = curren/1000/60;
return currenM;
}
}
Android日期和时间获取和转换
最新推荐文章于 2021-05-27 15:00:38 发布