date轉String
public static String dateToString(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
return format.format(date);
}
String轉date
public static Date stringToDate(String string) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date date = null;
try {
date = sdf.parse(string);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
date轉時間戳
/**
* 獲取系統時間戳
*
* @return
*/
public static long getCurrentTimeMillis() {
long currTimeSeconds = System.currentTimeMillis();
return currTimeSeconds;
}
date.getTime();
}
時間戳轉date
//暫時
SimpleDateFormat format = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Long time=newLong(445555555);
String d = format.format(time);
Date date=format.parse(d);
date轉calendar
Date date = new Date()
Calendar cal = Calendar.getInstance()
cal.setTime(date)
//對calendar進行操作
Calendar calendar=Calendar.getInstance()
calendar.setTime(date)
int year = calendar.get(calendar.YEAR)
int month = calendar.get(calendar.MONTH) + addMonthTime
int day = calendar.get(calendar.DAY_OF_MONTH) + addDayTime
calendar.set(year, month, day)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日")
String dateStr = sdf.format(calendar.getTime())
mTimeTv.setText(dateStr)
calendar轉date
Calendar cal=Calendar.getInstance();
Date() date=cal.getTime();
String轉時間戳
public static long stringToTimeMillis(String timeString) {
String timeStamp = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
Date d;
long l = 0;
try {
d = sdf.parse(timeString);
l = d.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return l;
}
時間戳轉String
public static String timeMillisToString(long millis, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String sd = sdf.format(new Date(millis));
return sd;
}
String轉calendar
String str="2012-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date =sdf.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar轉String
Calendar calendat = Calendar.getInstance()
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd")
String dateStr = sdf.format(calendar.getTime())