字符型参数转换成时间的问题,(在线等) 我想传入一个字符串参数,例如是“2008-3-3 16:04”,问:怎么判断他比当前时间比较是不是过期了?没有过期的话,返回传入参数对应的Calendar,这个是用什么函数?知道的麻烦贴下代码,谢谢了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 判断的话先date型,用SimpleDateFormater吧 Calendar calendar = Calendar. getInstance(); Date date = new Date(); calendar.setTime(date); 时间格式做下处理变为 2008-3-3 16:04:00import java.sql.Timestamp;import java.text.SimpleDateFormat;public class Test5 { private static String dateString = "2008-3-3 16:04:00"; public static void main(String args[]) { Timestamp t1 = Test5.getCourrentTimeByFormat(dateString); Timestamp t2 = Test5.getCourrentTime_Timestamp(); if (t1.after(t2)) { System.out.println(t1); } else { System.out.println(t2); } } public static Timestamp getCourrentTimeByFormat(String format) { SimpleDateFormat sDateFormat = new SimpleDateFormat(format); String todayTime = sDateFormat.format(java.util.GregorianCalendar .getInstance().getTime()); return Timestamp.valueOf(todayTime); } public static Timestamp getCourrentTime_Timestamp() { return new Timestamp(System.currentTimeMillis()); }} import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class Test { public Calendar test() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date date = df.parse("2008-3-3 16:04:05"); Calendar calendar = Calendar.getInstance(); Date now = calendar.getTime(); if(now.compareTo(date) > 0) { calendar.setTime(date); return calendar; } } catch (ParseException e) { e.printStackTrace(); } return null; } public static void main(String[] args) { new Test().test(); }} import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class Test { public Calendar test(String dateStr) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date date = df.parse(dateStr); Calendar calendar = Calendar.getInstance(); Date now = calendar.getTime(); if(now.compareTo(date) > 0) { calendar.setTime(date); return calendar; } } catch (ParseException e) { e.printStackTrace(); } return null; } public static void main(String[] args) { Calendar test = new Test().test("2008-3-3 16:04:05"); System.out.println(test.getTime()); }} 没过期返回Calendar,过期返回null。 public Calendar getNoExpiredCalendar(String dateStr) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar calendar = null; try { Date date = sdf.parse(dateStr); Date now = new Date(); if (!date.before(now)) { calendar = Calendar.getInstance(); calendar.setTime(date); } } catch (Exception e) { e.printStackTrace(); } return calendar; } 关于直接选择排序的问题,我哪错了啊??? 求助:netbean生成项目乱码 Java怎么调取一个网页啊? java有没有传函数指针的相当功能? 大家都来复习复习,看看这个是怎么回事(1) 关于数据库中的数据类型转换的问题 JBuilder9中用Weblogic7连接数据库,程序部分的lookup不能找到JNDI 搜索10次有9次显示超时!faint Java兩道簡單題,求助!!! 大虾们帮我啊! 请问关于使用java为db2创建触发器问题。谢谢各位。 现在的硬盘好假。
用SimpleDateFormater吧
Date date = new Date();
calendar.setTime(date);
import java.text.SimpleDateFormat;public class Test5 {
private static String dateString = "2008-3-3 16:04:00"; public static void main(String args[]) {
Timestamp t1 = Test5.getCourrentTimeByFormat(dateString);
Timestamp t2 = Test5.getCourrentTime_Timestamp();
if (t1.after(t2)) {
System.out.println(t1);
} else {
System.out.println(t2);
}
} public static Timestamp getCourrentTimeByFormat(String format) {
SimpleDateFormat sDateFormat = new SimpleDateFormat(format);
String todayTime = sDateFormat.format(java.util.GregorianCalendar
.getInstance().getTime());
return Timestamp.valueOf(todayTime);
} public static Timestamp getCourrentTime_Timestamp() {
return new Timestamp(System.currentTimeMillis());
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test { public Calendar test() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = df.parse("2008-3-3 16:04:05");
Calendar calendar = Calendar.getInstance();
Date now = calendar.getTime();
if(now.compareTo(date) > 0) {
calendar.setTime(date);
return calendar;
}
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
new Test().test();
}
}
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test { public Calendar test(String dateStr) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = df.parse(dateStr);
Calendar calendar = Calendar.getInstance();
Date now = calendar.getTime();
if(now.compareTo(date) > 0) {
calendar.setTime(date);
return calendar;
}
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
Calendar test = new Test().test("2008-3-3 16:04:05");
System.out.println(test.getTime());
}
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = null;
try {
Date date = sdf.parse(dateStr);
Date now = new Date();
if (!date.before(now)) {
calendar = Calendar.getInstance();
calendar.setTime(date);
}
} catch (Exception e) {
e.printStackTrace();
}
return calendar;
}