Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - SimpleDateFormat parses 12:19:00 to 00:19:00

I am trying to parse a string to date, but the output look incorrect: Below is my code.

public static void main(String[] args){      
  Date startDate = new Date();      
  DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy hh:mm:ss");

  try {
    startDate = (Date) formatter.parse("07.10.2012 12:19:24");
  } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }

  System.out.println("Formatted Date " + startDate.toString());
}

Program Output:
Formatted Date Sun Oct 07 00:19:24 IST 2012

Expected Output:
Formatted Date Sun Oct 07 12:19:24 IST 2012

like image 402
vinay shettar Avatar asked Nov 06 '12 06:11

vinay shettar


People also ask

What does SimpleDateFormat parse do?

The parse() Method of SimpleDateFormat class is used to parse the text from a string to produce the Date. The method parses the text starting at the index given by a start position.

What is the format of SimpleDateFormat?

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.

Is SimpleDateFormat can be used in multithreading?

2.2. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally. So SimpleDateFormat instances are not thread-safe, and we should use them carefully in concurrent environments.

How do you change date format to MM DD YYYY in Java?

Creating A Simple Date Format A SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. String pattern = "yyyy-MM-dd" ; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); The specified parameter “pattern” is the pattern used for formatting and parsing dates.


1 Answers

You might want to use 24h format instead of 12h format...

H Hour in day (0-23) Number 0

h Hour in am/pm (1-12) Number 12

Yes, formatting characters are case sensitive.

like image 94
Has QUIT--Anony-Mousse Avatar answered Sep 30 '22 03:09

Has QUIT--Anony-Mousse