0

My input in this case is 2012-09-28 but I receive 01/01/2011

I would like to receive 09/28/2012

main(){
 val scan = Scanner(System.`in`)
    val originalFormat: DateFormat = SimpleDateFormat("YYYY-MM-DD", Locale.ENGLISH)
    val targetFormat: DateFormat = SimpleDateFormat("MM/DD/YYYY")
    val date = originalFormat.parse(scan.next())
    val formattedDate = targetFormat.format(date)
    println(formattedDate)
}

What is my code missing?

GigaCoder
  • 177
  • 8
  • 1
    Your code is missing a modern library for dates and times, but leaving that aside, your patterns look suspicous: only upper-case letters... Those patterns are case sensitive, `y` for example is different from `Y`. – deHaar Sep 16 '21 at 13:42
  • 2
    Does this answer your question? [Add colon to 24 hour time in Java?](https://stackoverflow.com/questions/20862558/add-colon-to-24-hour-time-in-java) (I know the title doesn’t sound related, but I nevertheless believe the core of the question is the same.) – Ole V.V. Sep 16 '21 at 14:38

3 Answers3

4

The modern API for parsing and formatting date and time is java.time, which was introduced with Java 8. You can either import the ThreeTenAbp or use Android API Desugaring in order to make it work in Android API versions below 26.

The following example uses java.time and considers the input of the two different formats you posted (one in your question and one as a comment to the first answer).

import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.Scanner

fun main() {
    val scan = Scanner(System.`in`)
    // create a formatter that parses the two different EXPECTED input formats
    val inputFormatter = DateTimeFormatter.ofPattern("[uu-MM-dd][uuuu-MM-dd]");
    // parse the input
    val localDate: LocalDate = LocalDate.parse(scan.next(), inputFormatter)
    // define a formatter with the desired output format
    val targetFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("MM/dd/uuuu")
    // then create a String with the desired output format
    val formattedDate: String = localDate.format(targetFormat)
    // and print it
    println(formattedDate)
}

The result for the inputs 12-09-30 or 2012-09-30 is 09/30/2012 in both cases.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
deHaar
  • 17,687
  • 10
  • 38
  • 51
2

Colud you try this way?

fun main() {
    val scan = Scanner(System.`in`)
    val originalFormat = SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH)
    val targetFormat = SimpleDateFormat("MM/dd/yyyy")
    val date = originalFormat.parse(scan.next())
    val formattedDate = targetFormat.format(date)
    println(formattedDate)
}

d is a day in the month. (ex_10)
D is a day in the year. (ex_189)

y is the year. (ex_1996; 96)
Y is week year. (ex_2009; 09)

Ji Sungbin
  • 891
  • 1
  • 10
  • 19
0

Use yyyy instead of YYYY and dd instead of DD.

DD is the day of year while dd is the day of month.

YYYY is the week year and yyyy is the regular year.

https://developer.android.com/reference/java/text/SimpleDateFormat?hl=en

laalto
  • 150,114
  • 66
  • 286
  • 303