方法组合使用可以等到更丰富的返回值哟。
当前时间戳 单位秒
public static Long curentTime()
{
return System.currentTimeMillis()/1000;
}
字符串转换成日期类型
public static Date stringToDate(String dateStr, String patterns)
{
if (null == dateStr || dateStr.equals(""))
return null;
if (null == patterns || "".equals(patterns))
patterns = "yyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(patterns);
Date d = null;
try {
d = sdf.parse(patterns);
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}
转换日期成时间戳
public static long getTime(Date d)
{
long l = 0;
if (null != d)
{
l = d.getTime();
}
return l;
}
时间戳转为日期类型 seconds 秒数
public static String timeStamp2Date(String seconds, String patterns) {
if(patterns == null || patterns.isEmpty())
patterns = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(patterns);
return sdf.format(new Date(Long.valueOf(seconds + "000")));
}
转换为时间(天,时:分:秒.毫秒)timeMillis是毫秒数
public static String formatDateTime(Long timeMillis)
{
long day = timeMillis/(24*60*60*1000);
long hour = (timeMillis/(60*60*1000)-day*24);
long min = ((timeMillis/(60*1000))-day*24*60-hour*60);
long s = (timeMillis/1000-day*24*60*60-hour*60*60-min*60);
long sss = (timeMillis-day*24*60*60*1000-hour*60*60*1000-min*60*1000-s*1000);
return (day>0?day+",":"")+hour+":"+min+":"+s+"."+sss;
}
获取两个日期之间的天数
public static double getDistanceOfTwoDate(Date begin, Date end)
{
long beforeTime = begin.getTime();
long afterTime = end.getTime();
return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
}
从某个日期到现在的天数
public static long pastDays(Date date)
{
long t = new Date().getTime()-date.getTime();
return t/(24*60*60*1000);
}
从某个日期到现在的小时数
public static long pastHour(Date date)
{
long t = new Date().getTime()-date.getTime();
return t/(60*60*1000);
}
从某个日期到现在的分钟数
public static long pastMinutes(Date date)
{
long t = new Date().getTime()-date.getTime();
return t/(60*1000);
}
返回昨天的日期
public static Date getYesterday()
{
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -1);
return c.getTime();
}
返回本月第一天
public static String getMonthFirstDay()
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
int value = cal.getGreatestMinimum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, value);
String str = sdf.format(cal.getTime());
return str;
}
返回本月最后一天
public static String getMonthEndDay() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, value);
String str = sdf.format(cal.getTime());
return str;
}
2001total visits,1visits today