0

I tried to convert a String to Date by the following way in Java:

public static Date dateConv1(String s){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss");
        String dateInString = s;
        Date date = new Date();

It converts the noon 12:00-12:59 as midnight i.e 00:00-00:59. Could soeone let me know how I should solve this issue?

Beata
  • 335
  • 2
  • 10
  • 21
  • @YCF_L hmmm sounds like an answer *to me*, with proper explanations – Eugene May 15 '18 at 13:14
  • @Eugene I think I makes a mistake with that comment, my bad I read the question in a mirror :(!! The question still not clear enough, the OP should explain more – Youcef LAIDANI May 15 '18 at 13:31
  • I recommend you don’t use `SimpleDateFormat`. That class is long outdated along with `Date` and notoriously troublesome too. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. May 15 '18 at 20:03

3 Answers3

1

Change your formatter:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

HH - means hours

Reach to: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html and see all the examples you need.

roeygol
  • 4,908
  • 9
  • 51
  • 88
0

The String is not correctly formated. hh means 1-12 hours. There are 4 way to format hours (hh, HH, kk, KK). Here you can read about the formatting: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Superluminal
  • 947
  • 10
  • 23
0
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss");

" hh " is for 12 hours format change it to

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Kushagra Misra
  • 461
  • 1
  • 7
  • 15