请教大家一个问题,关于日期处理的,急用。请大家多帮忙!
现有一个字符串String str ="Mon Sep 07 15:48:06 CST 2009"怎么把它转化成java.util.Date()格式
显示输出为2009-9-07 15:48:06
用simpleDateFormat.parse(str)转化为日期格式但输出为Mon Sep 07 15:48:06 CST 2009;不是我想要的结果。
请大家多帮忙。非常感谢!!!
现有一个字符串String str ="Mon Sep 07 15:48:06 CST 2009"怎么把它转化成java.util.Date()格式
显示输出为2009-9-07 15:48:06
用simpleDateFormat.parse(str)转化为日期格式但输出为Mon Sep 07 15:48:06 CST 2009;不是我想要的结果。
请大家多帮忙。非常感谢!!!
解决方案 »
- 关于Hibernate Session得问题
- iBtais的select语句中resultClass是java.util.HashMap返回的键值为什么都是大写的
- 前一个帖子竟然沉了,小菜鸟问路,webservice客户端,怎么弄?
- ajax
- JSP中用Ajax实现自动补全功能
- jsp中的setAttribute
- 求Struts2.0.11+Spring2.0+Hibernate3.1的框架
- 请问在哪能弄到spring-sandbox.jar啊?
- 使用javac在windows的dos模式,编译数据链接mssql server2000数据的问题
- SSH框架 一个小问题 希望先驱给个思路吧
- java replace怎么样使用正则表达式
- 用el表达式迭代出text,在后台怎么取text的值
是不是应该先用.parse转换成日期型后,再用simpleDateFormat.format(Date)这个转换成字符串型,就是你要的了,simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
怎么转换为java.util.Date()格式 显示输出为2009-9-07 15:48:06
想string转date 比较困难
f.parse(str)
public static void main(String[] s){
Locale.setDefault(Locale.US);
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
System.out.println(df.format(df1.parse("2009-9-07 15:48:06")));
System.out.println(df1.format(df.parse("Mon Sep 07 15:48:06 CST 2009")));
} catch (ParseException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
但输出的格式为Mon Sep 07 15:48:06 CST 2009,在SQL查询时会出错的
我想要的结果的格式是2009-9-07 15:48:06
要得到“2009-9-06 15:48:06 ”得到的格式是java.util.Date()不要String类型的。请大家多帮忙!
public static void main(String[] args) throws ParseException {
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = "2009-9-07 15:48:06 " ;
String time = simple.format(simple.parse(str));
System.out.println(time); }
楼主看看这样可以吗?
没有必要非的先在类中把字符串的日期转换为Date 类型,在做笔记。
select * from oper_manager
where convert(varchar, end_date,120 )> '2012-01-01' 注: 1:其中的120 的意思就是标示 后面的格式20 或 120 (*) ODBC 规范 yyyy-mm-dd hh:mm:ss[.fff]
2:'2012-01-01' 为自己的参数,可以带时分秒 3:有关convert 的详细 介绍,请参考sql 的帮助文档。
Date date = new Date();//得到当前时间
long myTime = date.getTime()/1000-24*3600;//date内存放的是一个long型的,表示1970 1月1日 0时0分到现在的毫秒数。
然后
date.setTime(myTime);就得到昨天这个时候的时间了。
如果要用它与数据库比较的话直接用也没问题的(估计你是用的hibernate吧)。
date.setTime(myTime*1000);//刚忘了把秒化为毫秒了
if (date == null) {
throw new IllegalArgumentException("The date must not be null");
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(calendarField, amount);
return c.getTime();
}
public static Date addDays(Date date, int amount) {
return add(date, Calendar.DAY_OF_MONTH, amount);
} 你传入负数就可以了。
这就是前一天的时间析构字符串: public static Date parseDate(String str, String[] parsePatterns) throws ParseException {
if (str == null || parsePatterns == null) {
throw new IllegalArgumentException("Date and Patterns must not be null");
}
SimpleDateFormat parser = null;
ParsePosition pos = new ParsePosition(0);
for (int i = 0; i < parsePatterns.length; i++) {
if (i == 0) {
parser = new SimpleDateFormat(parsePatterns[0]);
} else {
parser.applyPattern(parsePatterns[i]);
}
pos.setIndex(0);
Date date = parser.parse(str, pos);
if (date != null && pos.getIndex() == str.length()) {
return date;
}
}
throw new ParseException("Unable to parse the date: " + str, -1);
}
import java.io.*;
import java.sql.Timestamp;public class Times{
public static void main(String[] args) throws Exception {
String ntime=null;
Calendar calendar=new GregorianCalendar();
calendar.setTimeInMillis( System.currentTimeMillis());
int year1=calendar.get(Calendar.YEAR);
int month1=calendar.get(Calendar.MONTH)+1;
int date1=calendar.get(Calendar.DATE);
int hour1=calendar.get(Calendar.HOUR_OF_DAY);
int min1=calendar.get(Calendar.MINUTE);
int sec1=calendar.get(Calendar.SECOND);
ntime=year1+"-"+month1+"-"+date1+" "+hour1+":"+min1+":"+sec1;
System.out.println(ntime);
}
}
select finsh_time from building where finsh_time between DATE('2009-09-03') and DATE_ADD('2009-09-09',INTERVAL 1 day);
很感谢大家的帮忙!
SimpleDateFormat format = new SimpleDateFormat(pattern);
Date ret = format.parse(dateStr);
2.日期回滚 /**
*
* @param now
* @param year
* @param month
* @param day
* @param hour
* @param minute
* @param second
* @return
*/
public static Date roll(Date now, int year, int month, int day, int hour,
int minute, int second) {
Calendar c = Calendar.getInstance();
c.setTime(now);
c.add(Calendar.YEAR, year);
c.add(Calendar.MONTH, month);
c.add(Calendar.DATE, day);
c.add(Calendar.HOUR_OF_DAY, hour);
c.add(Calendar.MINUTE, minute);
c.add(Calendar.SECOND, second);
if (Date.class == now.getClass())
return c.getTime();
return newInstance(now.getClass(), c.getTimeInMillis());
}代码中roll函数为日期回滚类,参数now为原始日期,后续参数为回滚得单位剩下自己搞定