java前几分钟的代码 java时间分段
用java如何实现 1小时前 1分钟前
/**
为牧野等地区用户提供了全套网页设计制作服务,及牧野网站建设行业解决方案。主营业务为网站设计、成都网站制作、牧野网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
* 显示时间,如果与当前时间差别小于一天,则自动用**秒(分,小时)前,如果大于一天则用format规定的格式显示
*
* @author wxy
* @param ctime
* 时间
* @param format
* 格式 格式描述:例如:yyyy-MM-dd yyyy-MM-dd HH:mm:ss
* @return
*/
public static String showTime(Date ctime, String format) {
String r = "";
if(ctime==null)return r;
if(format==null)format="yyyy-MM-dd HH:mm";
long nowtimelong = System.currentTimeMillis();
long ctimelong = ctime.getTime();
long result = Math.abs(nowtimelong - ctimelong);
if (result 60000)// 一分钟内
{
long seconds = result / 1000;
r = seconds + "秒钟前";
} else if (result = 60000 result 3600000)// 一小时内
{
long seconds = result / 60000;
r = seconds + "分钟前";
} else if (result = 3600000 result 86400000)// 一天内
{
long seconds = result / 3600000;
r = seconds + "小时前";
} else// 日期格式
{
r = DateTime.formatTime(ctime, format);
}
return r;
}
如何用java 获取15分钟以前的时间
代码如下:
1.得到当前的时间
Calendar cal = Calendar.getInstance();
String time=formatter.format(cal.getTime());
String strEnd=time.substring(0, 16);// 2007-10-30 09:30
2.得到前一天的时间
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_MONTH, -1);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String mDateTime=formatter.format(c.getTime());
String strStart=mDateTime.substring(0, 16);//2007-10-29 09:30
3.得到前几天的时间
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_MONTH, -5);//得到前5
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String mDateTime=formatter.format(c.getTime());
String strStart=mDateTime.substring(0, 16);//2007-10-24 09:30
4.得到当前时间的前几年的时间
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -5);//得到前5年
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String mDateTime=formatter.format(c.getTime());
String strStart=mDateTime.substring(0, 16);//2002-10-24 09:30
java8判断几秒前,几分钟前,几天前
看下对你是否有用
public class RelativeDateFormat {
private static final long ONE_MINUTE = 60000L;
private static final long ONE_HOUR = 3600000L;
private static final long ONE_DAY = 86400000L;
private static final long ONE_WEEK = 604800000L;
private static final String ONE_SECOND_AGO = "秒前";
private static final String ONE_MINUTE_AGO = "分钟前";
private static final String ONE_HOUR_AGO = "小时前";
private static final String ONE_DAY_AGO = "天前";
private static final String ONE_MONTH_AGO = "月前";
private static final String ONE_YEAR_AGO = "年前";
public static void main(String[] args) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:m:s");
Date date = format.parse("2013-11-11 18:35:35");
System.out.println(format(date));
}
public static String format(Date date) {
long delta = new Date().getTime() - date.getTime();
if (delta 1L * ONE_MINUTE) {
long seconds = toSeconds(delta);
return (seconds = 0 ? 1 : seconds) + ONE_SECOND_AGO;
}
if (delta 45L * ONE_MINUTE) {
long minutes = toMinutes(delta);
return (minutes = 0 ? 1 : minutes) + ONE_MINUTE_AGO;
}
if (delta 24L * ONE_HOUR) {
long hours = toHours(delta);
return (hours = 0 ? 1 : hours) + ONE_HOUR_AGO;
}
if (delta 48L * ONE_HOUR) {
return "昨天";
}
if (delta 30L * ONE_DAY) {
long days = toDays(delta);
return (days = 0 ? 1 : days) + ONE_DAY_AGO;
}
if (delta 12L * 4L * ONE_WEEK) {
long months = toMonths(delta);
return (months = 0 ? 1 : months) + ONE_MONTH_AGO;
} else {
long years = toYears(delta);
return (years = 0 ? 1 : years) + ONE_YEAR_AGO;
}
}
private static long toSeconds(long date) {
return date / 1000L;
}
private static long toMinutes(long date) {
return toSeconds(date) / 60L;
}
private static long toHours(long date) {
return toMinutes(date) / 60L;
}
private static long toDays(long date) {
return toHours(date) / 24L;
}
private static long toMonths(long date) {
return toDays(date) / 30L;
}
private static long toYears(long date) {
return toMonths(date) / 365L;
}
}
网页题目:java前几分钟的代码 java时间分段
链接地址:http://myzitong.com/article/dohhjpc.html