Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Date Formatter

I am getting date format as "YYYY-mm-dd hh:mm" as formatter object.

How can I format the input formatter object to get only "YYYY-mm-dd";?

like image 866
rajputhch Avatar asked Apr 07 '11 12:04

rajputhch


2 Answers

Format formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Date date;
        try {
            date = (Date)((DateFormat) formatter).parse("2011-04-13 05:00");
            formatter = new SimpleDateFormat("yyyy-MM-dd");
            String s = formatter.format(date);
            System.out.println(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
like image 142
Nirmal- thInk beYond Avatar answered Sep 28 '22 02:09

Nirmal- thInk beYond


I am getting date format as "YYYY-mm-dd hh:mm" as formatter object. How can i format the input formatter object to get only "YYYY-mm-dd";

You can not have date as YYYY-mm-dd it should be yyyy-MM-dd. To get date in yyyy-MM-dd following is the code:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(todaysDate);
like image 23
Harry Joy Avatar answered Sep 28 '22 02:09

Harry Joy