[英]date format issue string to java util date
我是Date函數的新手。 我的日期為字符串'03 / 10/2014' - 月/日/年。 我需要將其轉換為'10 -MAR-14'工具日期格式。 我該怎么做才能幫助我。
我使用'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。 這不會給出結果。 我需要這個轉換為休眠標准搜索。 數據庫有結果。 但是這種轉換並沒有給出結果。
你錯過了表達式中的y
,使用MM/dd/yyyy
以下代碼將創建一個方法並將其用於您的邏輯
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()));
}
}
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
Date date = new Date();
String format = dateFormat.format(date);
System.out.print("format is:"+format);
只需按照這個簡單
似乎MSDN庫有一個可以下載到您的項目的格式實用程序。 請參閱鏈接
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.