/** 时间小知识:
y年M月d日 H时m分s秒,这是字母分别代表的时间,
在转换时,如果只填一个字母,则即使那个时间只有一个数字也不会有多余的0。
比如用y年M月d日 H时m分s秒 来转换时间戳,转换的时间是2017年3月5天 3时8分4秒
比如用yyyy年MM月dd日 HH时mm分ss秒 来转换时间戳,转换的时间是2017年03月05天 03时08分04秒*/
//将十位数时间戳转换为时间日期
public String times(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("y年M月d日 H时m分s秒");
@SuppressWarnings("unused")
long lcc = Long.valueOf(time);
int i = Integer.parseInt(time);
String times = sdr.format(new Date(i * 1000L));
return times;
}
// 毫秒数转成给定格式的时间
public static String longToDate(String format, long time) {
SimpleDateFormat formatter = new SimpleDateFormat(format);
Date curDate = new Date(time);// 获取当前时间
return formatter.format(curDate);
}
/** 时间戳转换成指定时间格式 */
public static String getFormatDate(long time, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format);
Date curDate = new Date(time);// 获取当前时间
return formatter.format(curDate);
}
// 毫秒数转成星期
public static String longToWeek(long time) {
String week = "星期";
Calendar c = Calendar.getInstance();
c.setTimeInMillis(time);
if (c.get(Calendar.DAY_OF_WEEK) == 1) {
week += "天";
}
if (c.get(Calendar.DAY_OF_WEEK) == 2) {
week += "一";
}
if (c.get(Calendar.DAY_OF_WEEK) == 3) {
week += "二";
}
if (c.get(Calendar.DAY_OF_WEEK) == 4) {
week += "三";
}
if (c.get(Calendar.DAY_OF_WEEK) == 5) {
week += "四";
}
if (c.get(Calendar.DAY_OF_WEEK) == 6) {
week += "五";
}
if (c.get(Calendar.DAY_OF_WEEK) == 7) {
week += "六";
}
return week;
}
/**
* 得到本周周一
*
* @return yyyy-MM-dd
*/
public static String getMondayOfThisWeek() {
Calendar c = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
if (day_of_week == 0)
day_of_week = 7;
c.add(Calendar.DATE, -day_of_week + 1);
return dateFormat.format(c.getTime());
}
/**
* 得到本周周日
*
* @return yyyy-MM-dd
*/
public static String getSundayOfThisWeek() {
Calendar c = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
if (day_of_week == 0)
day_of_week = 7;
c.add(Calendar.DATE, -day_of_week + 7);
return dateFormat.format(c.getTime());
}
/**
* 获取当月的 天数
*
* @return
*/
public static int getCurrentMonthDays() {
Calendar a = Calendar.getInstance();
a.set(Calendar.DATE, 1);
a.roll(Calendar.DATE, -1);
int maxDate = a.get(Calendar.DATE);
return maxDate;
}
/**
* 获取当月最后一天的时间
*
* @return
*/
public static String getLastDayOfThisMonth() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar ca = Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH,
ca.getActualMaximum(Calendar.DAY_OF_MONTH));
String lastDay = format.format(ca.getTime());
return lastDay;
}
/**
* 获取当月第一天的时间
*
* @return
*/
public static String getFirDayOfThisMonth() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
String firstDay = format.format(c.getTime());
return firstDay;
}
时间与时间戳的转换
最新推荐文章于 2024-06-27 19:52:56 发布