2

I am converting Date to string format in yyyy-MM-dd HH:mm:ss format to save in sqlite database below is object declared for simple date format

public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Sometime it prepends pair of zeros in date as you can see in below image

Example Wrong date returns as as below

2018-08-25 02:32:0000

2018-08-25 02:32:0049

2018-0008-25 02:32:50

2018-08-24 0023:32:50

2018-08-0024 23:32:50 enter image description here

I have created custom funtion to correct this wrong date. But I want to know exact cause of this issue.

below is the code

public static String getCurrentDateTime() {
    Date d = new Date();

    String datetime = sdf.format(d);
    if (datetime.length() > 19) {
        datetime = correctDate(datetime);
    }
    return datetime;
}
Deepak Sharma
  • 409
  • 3
  • 14
  • Please include code content for your question, not as pictures. – Dragonthoughts Aug 24 '18 at 08:37
  • Does this behavior always happen? – Son Truong Aug 24 '18 at 08:41
  • Can you please initialize the `sdf` inside the function so that we can have the assurance if the `sdf` has not changed during other operations in your code? – Reaz Murshed Aug 24 '18 at 09:04
  • And what Java version you are using? – Reaz Murshed Aug 24 '18 at 09:04
  • Why you taken SimpleDateFormat as a static? – Mayur Patel Aug 24 '18 at 09:12
  • @sontruongit This is intermittent issue. Some time it happens – Deepak Sharma Aug 24 '18 at 09:45
  • Probably a result of using the date format in more than one thread. The good solution is to use java.time (through [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP)) and its `DateTimeFormatter`, it is thread safe. If you don’t want to do that, create a separate `SimpleDateFormat` for each thread. – Ole V.V. Aug 24 '18 at 09:49
  • As an aside, in any case 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. Aug 24 '18 at 11:05

2 Answers2

1

I'm sure that if you don't use that static instance of SimpleDateFormat you will have no problem:

public static String getCurrentDateTime() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date d = new Date();
    String datetime = sdf.format(d);
    return datetime;
}

See these links:
Why is Java's SimpleDateFormat not thread-safe?
"Java DateFormat is not threadsafe" what does this leads to?

  • I am using static variable cause I am converting date in specific time zone. Every user has his own timezone. So he can set time zone of app. So If I use SimpleDateFormat in method then I have to set Time zone as well. It May cause performance issue. So I set TimeZone once application loads and use sdf object to format date – Deepak Sharma Aug 24 '18 at 13:20
  • What performance issue? You're facing a serious problem and you're trying to avoid a simple in method call? –  Aug 24 '18 at 13:26
  • I have to use SimpleDateFormat sdfLocal = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdfLocal.setTimeZone(TimeZone.getTimeZone(Utility.TimeZoneId)); will it make any performance issue. As I am picking date every second ?? – Deepak Sharma Aug 24 '18 at 13:29
  • I'm not sure that you need it, but if a call that costs less than a millisecond is the only issue then you must make it –  Aug 24 '18 at 13:33
  • I guess its working dude? I am testing it. I am not sure about performance hit by adding timezone. But its working. Do you know why its adding zeros ? – Deepak Sharma Aug 24 '18 at 13:39
  • What is adding zeros? –  Aug 24 '18 at 13:40
  • You mean your old static version of SimpleDateFormat? –  Aug 24 '18 at 13:41
  • Yes. That was returing wrong date e.g 2018-0008-25 02:32:50, 2018-08-24 0023:32:50. Its adding zero before some time day, month, year and some time adding zero to all component of datetime. But I want to know why its behaving like this. – Deepak Sharma Aug 24 '18 at 13:43
  • No I don't know about that. I read 2 or 3 links from SO stating that SimpleDateFormat is not thread safe, so I guess a static instance variable falls in that case too. Otherwise it does not make sense. –  Aug 24 '18 at 13:45
  • As to why it is behaving that way, the short answer is it wasn’t designed to be thread safe, so when using it from two or more threads, behaviour is unspecified and any result may occur. There’s much more information in the links. – Ole V.V. Aug 24 '18 at 18:22
0

SimpleDateFormat is not thread safe that's why you should check if in your calling code there is no issue with that.

  • Why its adding zero. What is role of threading in adding zeros in date – Deepak Sharma Aug 24 '18 at 13:19
  • Maybe you have to check why it's doing so while multiple threads are accessing the SimpleDateFormat? Is it multiple threaded? –  Aug 27 '18 at 08:09