Get day, month and year separately using SimpleDateFormat

69,879

Solution 1

If you need to get the values separately, then use more than one SimpleDateFormat.

SimpleDateFormat dayFormat = new SimpleDateFormat("dd");
String day = dayFormat.format(Date.parse(payback.creationDate.date));

SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
String month = monthFormat .format(Date.parse(payback.creationDate.date));

etc.

Solution 2

    SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy  hh:mm", Locale.ENGLISH);

    Date theDate = format.parse("JAN 13,2014  09:15");

    Calendar myCal = new GregorianCalendar();
    myCal.setTime(theDate);

    System.out.println("Day: " + myCal.get(Calendar.DAY_OF_MONTH));
    System.out.println("Month: " + myCal.get(Calendar.MONTH) + 1);
    System.out.println("Year: " + myCal.get(Calendar.YEAR));

Solution 3

Wow, SimpleDateFormat for getting string parts? It can be solved much easier if your input string is like "Jan,23,2014":

String input = "Jan,23,2014";
String[] out = input.split(",");
System.out.println("Year = " + out[2]);
System.out.println("Month = " + out[0]);
System.out.println("Day = " + out[1]);

Output:

Year = 2014
Month = Jan
Day = 23

But if you really want to use SimpleDateFormat because of some reason, the solution will be the following:

String input = "Jan,23,2014";
SimpleDateFormat format = new SimpleDateFormat("MMM,dd,yyyy");
Date date = format.parse(input);
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
calendar.setTime(date);
System.out.println(calendar.get(Calendar.YEAR));
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
System.out.println(new SimpleDateFormat("MMM").format(calendar.getTime()));

Output:

2014
23
Jan

Solution 4

tl;dr

Use LocalDate class.

LocalDate
.parse(
    "Jan,23,2014" , 
    DateTimeFormatter.ofPattern( "MMM,dd,uuuu" , Locale.US )
)
.getYear()

… or .getMonthValue() or .getDayOfMonth.

java.time

The other Answers use outmoded classes. The java.time classes supplant those troublesome old legacy classes.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

String input = "Jan,23,2014";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MMM,d,uuuu" );
LocalDate ld = LocalDate.parse( input , f );

Interrogate for the parts you want.

int year = ld.getYear();
int month = ld.getMonthValue();
int dayOfMonth = ld.getDayOfMonth();

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Solution 5

Use this to parse "Jan,23,2014"

SimpleDateFormat fmt = new SimpleDateFormat("MMM','dd','yyyy"); 
Date dt = fmt.parse("Jan,23,2014");

then you can get whatever part of the date.

Share:
69,879
John Error
Author by

John Error

Updated on July 17, 2022

Comments

  • John Error
    John Error almost 2 years

    I have a SimleDateFormat like this

    SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy  hh:mm");
    String date = format.format(Date.parse(payback.creationDate.date));
    

    I'm giving date with the format like "Jan,23,2014".

    Now, I want to get day, month and year separately. How can I implement this?