java 时间处理 转 Calendar 调用 compareTo方法比较 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public static void main(String[] args) throws Exception { String str_0 = "20130505"; String str_1 = "20130506"; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Date d_0 = sdf.parse(str_0); Date d_1 = sdf.parse(str_1); Calendar ca_0 = Calendar.getInstance(); Calendar ca_1 = Calendar.getInstance(); ca_0.setTime(d_0); ca_1.setTime(d_1); System.out.println(ca_0.compareTo(ca_1)); } 改进一下 String time = "05:00:00"; SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse(time)); Calendar now = Calendar.getInstance(); cal.set(Calendar.YEAR, now.get(Calendar.YEAR)); cal.set(Calendar.MONTH, now.get(Calendar.MONTH)); cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH)); // Within two minutes if (now.getTimeInMillis() - cal.getTimeInMillis() < 2 * 60 * 1000) { System.out.println("Time is up."); } package com.jst.util;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class DateUtil { public static String formart(Date date, String pattern) { if (date == null) { return null; } if (StringUtil.isEmpty(pattern)) { return formart(date); } SimpleDateFormat dateFormat = new SimpleDateFormat(pattern); return dateFormat.format(date); } public static String formart(Date date) { if (date == null) { return null; } SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return dateFormat.format(date); } public static Date parse(String str) throws ParseException { if (str == null || str.trim().equals("")) { return null; } SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return dateFormat.parse(str); } public static String getNowTime() { return formart(new Date()); } public static long millisecondBetween(Date startDate, Date endDate) { Calendar startCalendar = Calendar.getInstance(); Calendar endCalendar = Calendar.getInstance(); startCalendar.setTime(startDate); endCalendar.setTime(endDate); setTimeToMidnight(startCalendar); setTimeToMidnight(endCalendar); // 获取毫秒 long startMinutes = startCalendar.getTimeInMillis(); // 获取毫秒 long endMinutes = endCalendar.getTimeInMillis(); long intervalMs = endMinutes - startMinutes; return intervalMs; } public static int minutesBetween(Date startDate, Date endDate) { Calendar startCalendar = Calendar.getInstance(); Calendar endCalendar = Calendar.getInstance(); startCalendar.setTime(startDate); endCalendar.setTime(endDate); setTimeToMidnight(startCalendar); setTimeToMidnight(endCalendar); // 获取毫秒 long startMinutes = startCalendar.getTimeInMillis(); // 获取毫秒 long endMinutes = endCalendar.getTimeInMillis(); long intervalMs = endMinutes - startMinutes; return millisecondsToMinutes(intervalMs); } public static int minutesBetweenByDay(Date startDate, Date endDate) { Calendar startCalendar = Calendar.getInstance(); Calendar endCalendar = Calendar.getInstance(); startCalendar.setTime(startDate); endCalendar.setTime(endDate); setTimeToMidnight(startCalendar); setTimeToMidnight(endCalendar); // 获取毫秒 long startMinutes = startCalendar.getTimeInMillis(); // 获取毫秒 long endMinutes = endCalendar.getTimeInMillis(); long intervalMs = endMinutes - startMinutes; return millisecondsToDay(intervalMs); } /** * 毫秒转换为分钟 * * @param intervalMs * @return */ private static int millisecondsToMinutes(long intervalMs) { return (int) (intervalMs / (1000 * 60)); } /** * 毫秒转换为天 * * @param intervalMs * @return */ private static int millisecondsToDay(long intervalMs) { return (int) (intervalMs / (1000 * 60 * 60 * 24)); } /** * 设置毫秒上的加减 * * @param calendar */ private static void setTimeToMidnight(Calendar calendar) { // calendar.set(Calendar.SECOND, 0); } public static String getString(Date date, String pattern) { if (date == null) { return null; } if (pattern == null || pattern.trim().equals("")) { return getString(date); } SimpleDateFormat dateFormat = new SimpleDateFormat(pattern); return dateFormat.format(date); } public static String getString(Date date) { if (date == null) { return null; } SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); return dateFormat.format(date); } public static Date parse(String str, String pattern) throws ParseException { if (str == null || str.trim().equals("")) { return null; } SimpleDateFormat dateFormat; if (pattern != null) { dateFormat = new SimpleDateFormat(pattern); } else { dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); } return dateFormat.parse(str); } public static void main(String[] args) throws InterruptedException { try { Date date1 = DateUtil.parse("2014-05-28 15:40", "yyyy-MM-dd HH:mm"); Thread.sleep(1300); long aa = minutesBetween(date1, new Date()); System.out.println(aa); } catch (ParseException e) { e.printStackTrace(); } }} 思路不对,求高手 关于接口 求Java swt教程 关于java中参数使用(String... name1)中的参数有什么特别的用处吗? 有段代码不明白,请解释,谢谢!分不够马上加!!!! java编程思想 第三版 习题讨教做法 简单的编程题 懂的都进啊 关于动态加载jar文件的问题 一个走入误区~的java初学者~~~~~~~~~~~~~~的迷惑~!有没有大虾帮帮忙啊谢谢啊~! 诚招java程序员 关于递归查询子节点 新手求助,望大神赐教!
public static void main(String[] args) throws Exception {
String str_0 = "20130505";
String str_1 = "20130506";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date d_0 = sdf.parse(str_0);
Date d_1 = sdf.parse(str_1);
Calendar ca_0 = Calendar.getInstance();
Calendar ca_1 = Calendar.getInstance();
ca_0.setTime(d_0);
ca_1.setTime(d_1);
System.out.println(ca_0.compareTo(ca_1));
}
String time = "05:00:00";
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(time));
Calendar now = Calendar.getInstance();
cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
cal.set(Calendar.MONTH, now.get(Calendar.MONTH));
cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH));
// Within two minutes
if (now.getTimeInMillis() - cal.getTimeInMillis() < 2 * 60 * 1000) {
System.out.println("Time is up.");
}
package com.jst.util;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;public class DateUtil {
public static String formart(Date date, String pattern) {
if (date == null) {
return null;
}
if (StringUtil.isEmpty(pattern)) {
return formart(date);
}
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
return dateFormat.format(date);
} public static String formart(Date date) {
if (date == null) {
return null;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat.format(date);
} public static Date parse(String str) throws ParseException {
if (str == null || str.trim().equals("")) {
return null;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat.parse(str);
} public static String getNowTime() {
return formart(new Date());
} public static long millisecondBetween(Date startDate, Date endDate) {
Calendar startCalendar = Calendar.getInstance();
Calendar endCalendar = Calendar.getInstance();
startCalendar.setTime(startDate);
endCalendar.setTime(endDate);
setTimeToMidnight(startCalendar);
setTimeToMidnight(endCalendar);
// 获取毫秒
long startMinutes = startCalendar.getTimeInMillis();
// 获取毫秒
long endMinutes = endCalendar.getTimeInMillis();
long intervalMs = endMinutes - startMinutes;
return intervalMs;
} public static int minutesBetween(Date startDate, Date endDate) {
Calendar startCalendar = Calendar.getInstance();
Calendar endCalendar = Calendar.getInstance();
startCalendar.setTime(startDate);
endCalendar.setTime(endDate);
setTimeToMidnight(startCalendar);
setTimeToMidnight(endCalendar);
// 获取毫秒
long startMinutes = startCalendar.getTimeInMillis();
// 获取毫秒
long endMinutes = endCalendar.getTimeInMillis();
long intervalMs = endMinutes - startMinutes;
return millisecondsToMinutes(intervalMs);
} public static int minutesBetweenByDay(Date startDate, Date endDate) {
Calendar startCalendar = Calendar.getInstance();
Calendar endCalendar = Calendar.getInstance();
startCalendar.setTime(startDate);
endCalendar.setTime(endDate);
setTimeToMidnight(startCalendar);
setTimeToMidnight(endCalendar);
// 获取毫秒
long startMinutes = startCalendar.getTimeInMillis();
// 获取毫秒
long endMinutes = endCalendar.getTimeInMillis();
long intervalMs = endMinutes - startMinutes;
return millisecondsToDay(intervalMs);
} /**
* 毫秒转换为分钟
*
* @param intervalMs
* @return
*/
private static int millisecondsToMinutes(long intervalMs) {
return (int) (intervalMs / (1000 * 60));
} /**
* 毫秒转换为天
*
* @param intervalMs
* @return
*/
private static int millisecondsToDay(long intervalMs) {
return (int) (intervalMs / (1000 * 60 * 60 * 24));
} /**
* 设置毫秒上的加减
*
* @param calendar
*/
private static void setTimeToMidnight(Calendar calendar) {
// calendar.set(Calendar.SECOND, 0);
} public static String getString(Date date, String pattern) {
if (date == null) {
return null;
}
if (pattern == null || pattern.trim().equals("")) {
return getString(date);
}
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
return dateFormat.format(date);
} public static String getString(Date date) {
if (date == null) {
return null;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return dateFormat.format(date);
} public static Date parse(String str, String pattern) throws ParseException {
if (str == null || str.trim().equals("")) {
return null;
}
SimpleDateFormat dateFormat;
if (pattern != null) {
dateFormat = new SimpleDateFormat(pattern);
} else {
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
}
return dateFormat.parse(str);
} public static void main(String[] args) throws InterruptedException {
try {
Date date1 = DateUtil.parse("2014-05-28 15:40", "yyyy-MM-dd HH:mm");
Thread.sleep(1300);
long aa = minutesBetween(date1, new Date());
System.out.println(aa);
} catch (ParseException e) {
e.printStackTrace();
} }
}