Setting date into “dd”-“mm”-“yy” format from “dd”-“mm”-“yyyy” in java

前端 未结 4 629
小蘑菇
小蘑菇 2020-12-22 13:52

I have a date in the format dd-mm-yyyy format(inside my db).I need to convert it to dd-mm-yy format.I retrieved date from db and stored it inside a string.

S         


        
相关标签:
4条回答
  • 2020-12-22 14:17

    Use DateFormat for that issue:

    DateFormat df = SimpleDateFormat("dd-MM-yyyy");
    DateFormat df1 = SimpleDateFormat("dd-MM-yy");
    df1.format(df.parse(date));
    
    0 讨论(0)
  • 2020-12-22 14:30

    Try below code

     try {
            SimpleDateFormat sdf= new SimpleDateFormat("MM-dd-yy");
            Date d = sdf.parse("05-19-1990");
            System.out.println(sdf.format(d));
        } catch (ParseException ex) {
           ex.printStackTrace();
        }
    
    0 讨论(0)
  • 2020-12-22 14:32

    Use two SimpleDateFormats, one to parse it, one to format it..

    SimpleDateFormat in = new SimpleDateFormat("MM-dd-yyyy");
    Date inDate = in.parse(date); // 05-19-1990
    SimpleDateFormat out = new SimpleDateFormat("MM-dd-yy");
    String newDate = out.format(inDate);
    

    or

    SimpleDateFormat out = new SimpleDateFormat("dd-MM-yy");
    String newDate = out.format(inDate);
    

    If you really want 19-05-90 as per the title of your question ;)

    Check java.text.SimpleDateFormat for more details

    0 讨论(0)
  • 2020-12-22 14:38

    Try this..

    DateFormat df2 = new SimpleDateFormat("dd-mm-yy");
    String formattedDate2 = df2.format(theDate);
    
    0 讨论(0)
提交回复
热议问题