1

I am converting string to date format year and date showing correct but month showing different.

String s = "2018-08-29"
try 
    {

        DateFormat formatter = new SimpleDateFormat("yyyy-MM-DD", Locale.ENGLISH);

        Date parse = formatter.parse(s);

        long time = parse.getTime();

    }catch (ParseException e) {

         e.printStackTrace();

    }

output: Mon Jan 29 00:00:00 GMT+05:30 2018

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
jyothish
  • 117
  • 1
  • 10
  • Where is the print statement? show the entire code please – Tharaka Devinda Sep 03 '18 at 04:56
  • output is the print statement parse.toString() – jyothish Sep 03 '18 at 05:00
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Sep 03 '18 at 06:03

2 Answers2

6

The problem is in your format.

D stands for day in year, but you need to get day in month with d.

This should be like this:

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

Source

I hope it's helpful

ali73
  • 415
  • 1
  • 5
  • 17
0

Sorry I missed it at first. Your date should be simple letters "dd" not "DD" DD is day in year dd is date in month

Tharaka Devinda
  • 1,952
  • 21
  • 23