简体   繁体   English

日期格式问题字符串到java util日期

[英]date format issue string to java util date

I am new to Date functions. 我是Date函数的新手。 i have a date as a string '03/10/2014' - month/date/year. 我的日期为字符串'03 / 10/2014' - 月/日/年。 i need to convert this like '10-MAR-14' util date format. 我需要将其转换为'10 -MAR-14'工具日期格式。 How can i do this please help me. 我该怎么做才能帮助我。

i used 'MM/dd/yyy' and 'dd-MMM-yyy' formats. 我使用'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);

to do this. 去做这个。 but gives me parse exception. 但是给了我解析异常。 Appreciate your help 感谢您的帮助

but database column has the date-Month-year eg: 09-MAR-14. 但数据库列具有日期 - 月 - 年,例如:09-MAR-14。 this does not give the result. 这不会给出结果。 i need to this conversion for hibernate criteria search. 我需要这个转换为休眠标准搜索。 Database has results. 数据库有结果。 but this conversion does not give the results. 但是这种转换并没有给出结果。

你错过了表达式中的y ,使用MM/dd/yyyy

The following code will make a method and use it your logic 以下代码将创建一个方法并将其用于您的逻辑

       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);

Just follow this Simply 只需按照这个简单

It seems that the MSDN library has a format utility that can be downloaded to your project. 似乎MSDN库有一个可以下载到您的项目的格式实用程序。 See the link 请参阅链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM