Java Date Formatter
Asked Answered
S

13

11

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

Smock answered 7/4, 2011 at 12:24 Comment(3)
You should accept one of the answers below as the solution of your problem, or update your question if you still have no answer.Perceptual
For new readers to the question I recommend you don’t use SimpleDateFormat and also not Date. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use LocalDateTime and DateTimeFormatter, both from java.time, the modern Java date and time API.Touchhole
See the much newer Answer that provides a modern solution using java.time classes in Java 8+.Ambrogio
N
25

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);
Nunn answered 7/4, 2011 at 12:28 Comment(1)
Take care that the SimpleDateFormat is not thread safe and is a common source of errors. Use the DateTimeFormatter insteadTreed
D
6
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();
        }
Dissonancy answered 7/4, 2011 at 12:29 Comment(0)
P
3

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)));
Perceptual answered 7/4, 2011 at 12:29 Comment(0)
T
2

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

Trapezoid answered 7/4, 2011 at 12:30 Comment(0)
F
1

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()) );
Ferrand answered 7/4, 2011 at 12:47 Comment(0)
T
1

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.

Touchhole answered 5/12, 2021 at 11:26 Comment(0)
H
0

SimpleDateFormat is what you're looking for.

Highkey answered 7/4, 2011 at 12:29 Comment(0)
S
0

Try this:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(todaysDate);
Seeley answered 18/3, 2013 at 11:0 Comment(0)
S
0

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");
Shiloh answered 20/12, 2013 at 5:28 Comment(0)
A
0

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
Ambrogio answered 20/12, 2013 at 7:2 Comment(0)
S
0

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;    
} 
Selena answered 19/5, 2015 at 10:55 Comment(1)
This question has so many bad answers. About all of them endorsing SimpleDateFormat, a notorious troublemaker of a class that we sort of had to use when the question was asked in 2011 but that no one should be using anymore.Touchhole
L
0
 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();
    }
Livelong answered 21/1, 2017 at 7:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.