我不熟悉Date函数。我有一个字符串'03/10/2014‘-月/日/年。我需要将其转换为'10-MAR-14‘util日期格式。我该怎么做呢?请帮帮我。
我使用了'MM/dd/yyy‘和'dd-MMM-yyy’格式。
public static Date convertStringToDate(String aMask, String strDate)
throws ParseException {
SimpleDateFormat df;
Date date;
df = new SimpleDateFormat(aMask);
if (log.isDebugEnabled()) {
log.debug("converting '" + strDate + "' to date with mask '" + aMask + "'");
}
try {
date = df.parse(strDate);
} catch (ParseException pe) {
//log.error("ParseException: " + pe);
throw new ParseException(pe.getMessage(), pe.getErrorOffset());
}
return (date);
要做到这一点。但却给了我一个解析异常。感谢您的帮助
但是数据库列的日期-月-年,例如: 09-MAR-14。这不会给出结果。我需要将此转换为hibernate标准搜索。数据库有结果。但此转换不会给出结果。
发布于 2014-03-10 18:58:57
表达式中缺少y
,请使用MM/dd/yyyy
发布于 2014-03-10 19:40:17
以下代码将创建一个方法并使用它作为您的逻辑
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class ex {
public static void main(String[] args) {
// TODO Auto-generated method stub
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-M-yyyy");
String dateInString = "15-08-2014";
Date date = null;
try {
date = sdf1.parse(dateInString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy");
Calendar calendar = new GregorianCalendar(date.getYear(),date.getMonth(),date.getDate());
System.out.println("Date :" + sdf.format(calendar.getTime()));
}
}`
发布于 2014-03-10 21:13:56
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
Date date = new Date();
String format = dateFormat.format(date);
System.out.print("format is:"+format);
只需简单地遵循这一点
https://stackoverflow.com/questions/22298237
复制相似问题