rajputhch
rajputhch

Reputation: 627

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";?

Upvotes: 11

Views: 94394

Answers (13)

Anonymous
Anonymous

Reputation: 86379

java.time

I recommend that you use java.time, the modern Java date and time API, for your date and time work.

For parsing input define a formatter:

private static final DateTimeFormatter FORMATTER
        = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm", Locale.ROOT);

Parse:

    String input = "2019-01-21 23:45";
    LocalDateTime dateTime = LocalDateTime.parse(input, FORMATTER);
    System.out.println(dateTime);

Output so far:

2019-01-21T23:45

Format output:

    String output = dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE);
    System.out.println(output);

2019-01-21

Tutorial link

Trail: Date Time (The Java™ Tutorials) explaining how to use java.time.

Upvotes: 1

ramya
ramya

Reputation: 1

 SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                String strDate = entry_date;
                System.out.println("strDate*************"+strDate);
                Date date = null;
                try {
                    date = sdf.parse(strDate);

                } catch (ParseException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

                Date yesterday =subtractDay( date);
                String requiredDate = df.format(yesterday);
                System.out.println("110 days before*******************"+requiredDate);

public static Date subtractDay(Date date) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, -110);`enter code here`
        return cal.getTime();
    }

Upvotes: 0

dharmendra
dharmendra

Reputation: 7881

This question has so many good answers !! , here comes another one more generic solution

public static String getDateInFormate(String oldFormate , String newFormate , String dateToParse){
    //old "yyyy-MM-dd hh:mm"
    //new yyyy-MM-dd
    //dateTopars 2011-04-13 05:00  
    String formatedDate="";
    Format formatter = new SimpleDateFormat();
    Date date;
    try {
        date = (Date)((DateFormat) formatter).parse(dateToParse);
        formatter = new SimpleDateFormat(newFormate);
        formatedDate = formatter.format(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return formatedDate;    
} 

Upvotes: 0

Basil Bourque
Basil Bourque

Reputation: 340158

Other answers such as the one by user2663609 are correct.

As an alternative, the third-part open-source replacement for the java.util.Date/Calendar classes, Joda-Time, includes a built-in format for your needs.

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;

String stringIn = "2011-04-07";

// Returns a formatter for a full date as four digit year, two digit month of year, and two digit day of month (yyyy-MM-dd).
DateTimeFormatter formatter = ISODateTimeFormat.date().withZone( DateTimeZone.forID( "Europe/London" ) ).withLocale( Locale.UK );
DateTime dateTime = formatter.parseDateTime( stringIn ).withTimeAtStartOfDay();
String stringOut = formatter.print( dateTime );

Dump to console…

System.out.println( "dateTime: " + dateTime.toString() );
System.out.println( "stringOut: " + stringOut );

When run…

dateTime: 2011-04-07T00:00:00.000+01:00
stringOut: 2011-04-07

Upvotes: 0

user2663609
user2663609

Reputation: 427

Use this code:

Date date=new Date();

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

String formattedDate = formatter.format(date);

System.out.println("formatted time==>" + formattedDate);

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

Upvotes: 0

joel
joel

Reputation: 11

Try this:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(todaysDate);

Upvotes: 0

Harry Joy
Harry Joy

Reputation: 59694

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

Upvotes: 25

Imran
Imran

Reputation: 3024

Following sample formate date as yyyy-MM-dd in Java

Format formatter = new SimpleDateFormat("yyyy-MM-dd");
Calendar now = Calendar.getInstance();
System.out.println("Now: "+formatter.format(now.getTime()) );

Upvotes: 1

aioobe
aioobe

Reputation: 421290

If you're getting a date in the format "YYYY-mm-dd hh:mm" and you want it as "YYYY-mm-dd" I suggest you just use inputDate.substring(0, 10).

In either way, beware of potential Y10k bugs :)

Upvotes: 2

Nirmal- thInk beYond
Nirmal- thInk beYond

Reputation: 12064

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

Upvotes: 6

Mike Yockey
Mike Yockey

Reputation: 4593

Use SimpleDateFormat

String myDateString = "2009-04-22 15:51";

SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");

System.out.println(outFormat.format(inFormat.parse(myDateString)));

Upvotes: 3

asgs
asgs

Reputation: 3984

SimpleDateFormat is what you're looking for.

Upvotes: 0

Related Questions