0

What is wrong with my code i couldn't get the exact output from the date time that i want to be parsed?

String convertDate="03/19/2014 5:30:10 PM";
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.CANADA);
Date myDate=new Date();
try {
    myDate=df.parse(convertDate);
    final Calendar c = Calendar.getInstance();
    c.setTime(myDate);
    System.out.println("Year = " + c.get(Calendar.YEAR));
    System.out.println("Month = " + (c.get(Calendar.MONTH)));

    System.out.println("Day = " + c.get(Calendar.DAY_OF_MONTH));
} catch (ParseException e1) {
    e1.printStackTrace();
}

And my output is

 - 04-28 03:07:15.322: I/System.out(25095): Year = 2015
 - 04-28 03:07:15.322: I/System.out(25095): Month = 6
 - 04-28 03:07:15.322: I/System.out(25095): Day = 3

it must be

 - Year = 2014
 - Month = 03
 - Day = 19
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
DreamBigAlvin
  • 884
  • 3
  • 13
  • 35

3 Answers3

2

Change your date format from this

dd/MM/yyyy HH:mm:ss

to this

MM/dd/yyyy HH:mm:ss

And month is Zero (0) index based. So, after retrieving month index you must have to add 1 to get current month.

So, try to print current month as follows...

int currentMonth = c.get(Calendar.MONTH) + 1;

System.out.println("Month = " + currentMonth);

Or

System.out.println("Month = " + (c.get(Calendar.MONTH)+1));
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
1
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.CANADA);

Now try... It'll work.

With this you have to add 1 with your month. code is here:

 String convertDate="03/19/2014 5:30:10 PM";
         DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.CANADA);
         Date myDate=new Date();
         try {
            myDate=df.parse(convertDate);
            final Calendar c = Calendar.getInstance();
            c.setTime(myDate);
            int y,m,d;
            y=Calendar.YEAR;
            System.out.println("Year = " + c.get(Calendar.YEAR));
            //Log.d("Year", y);
            System.out.println("Month = " + (c.get(Calendar.MONTH)+1));

            System.out.println("Day = " + c.get(Calendar.DATE));
        } catch (ParseException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
  • 1
    "MM/dd/yyyy HH:mm:ss" is not a format for "03/19/2014 5:30:10 PM" also the result still wrong, this time Month = 2 it should be Month = 3 – Eng. Samer T Apr 28 '14 at 03:34
  • what do you mean to format my code..? i already change my Formatter to DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss aa", Locale.CANADA); – DreamBigAlvin Apr 28 '14 at 03:52
1

Unfortunately, the accepted answer as well the other answer are wrong.

You have done two mistakes in parsing:

  1. You have swapped MM with dd.
  2. You have used H instead of h to parse a date-time string with AM/PM marker. The symbol, H is used to parse a time string in 24-Hour format. Another important point to consider is that PM is written as p.m. in Locale.CANADA. For your time string, you can use Locale.US which specifies it as PM.

Thus, you can use a parser like the one given below:

new SimpleDateFormat("M/d/u h:m:s a", Locale.US)

where a has been used to parse AM/PM.

Using this parser will fix your problem but the legacy date-time API (java.util date-time types and their formatting API, SimpleDateFormat) is outdated and error-prone. It is recommended to stop using it completely and switch to java.time, the modern date-time API*.

Demo using modern date-time API:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String args[]) {
        String convertDate = "03/19/2014 5:30:10 PM";
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("M/d/u h:m:s a", Locale.US);
        LocalDateTime ldt = LocalDateTime.parse(convertDate, dtfInput);
        System.out.printf("Year: %d, Month: %d, Day: %d%n", ldt.getYear(), ldt.getMonthValue(), ldt.getDayOfMonth());

        // Or, using DateTimeFormatter
        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("'Year:' uuuu, 'Month:' M, 'Day:' d", Locale.US);
        System.out.println(ldt.format(dtfOutput));
    }
}

Output:

Year: 2014, Month: 3, Day: 19
Year: 2014, Month: 3, Day: 19

Learn more about the the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110