Simple Date format returns Wrong date intermittently
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?
SimpleDateFormat producing wrong date time when parsing YYYY-MM-dd HH:mm
YYYY should be yyyy-
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.ENGLISH);
Please check the documentation for SimpleDateFormat here
Java 6 : http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
Java 7 : http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
SimpleDateFormat sometimes wrong hours formatting
193500000 -> 05hours:45minutes (Wrong, correct is 53hours:45minutes)
That's because 53 hours = 2 days and 5 hours
60300000 -> 16hours:45minutes (correct)
63900000 -> 17hours:45minutes (correct)
108000000-> 06hours:00minutes (Wrong, correct is 30hours:00minutes)
That's because 30 hours = 1 day and 6 hours
117000000 -> 08hours:30minutes (Wrong, correct is 32hours:30minutes)
That's because 32 hours = 1 day and 8 hours
If you only want to calculate the number of hours from ms:
193500000/(1000*60*60) = number of hours
(193500000/(1000*60))%60 = number of remaining minutes
SimpleDateFormat returning incorrect day on given date
You should be using SimpleDateFormat("dd-MM-yyyy")
.
DD is for day in year, like there are 365 days in a year.
Java simpledateformat.parse is giving incorrect year
If you use your code in a multi-threaded scenario, you may get the wrong result because SimpleDateFormat
is not a thread-safe class. If you use Java8+ use DateTimeFormatter
instead. Here is a test code to verify SimpleDateFormat
is not thread-safe class, hope helpful.
public class MultiThreadSimpleDateFormatClient {
public static void main(String[] args) {
HandleDate handleDate = new HandleDate();
Random random = new Random();
Set<String> randomStrs = new HashSet<>();
Thread thread1 = new Thread(() -> {
while (true) {
int partOfYear = random.nextInt(10);
handleDate.verifyNotThreadSafe("201" + partOfYear + "1115040613555");
}
});
Thread thread2 = new Thread(() -> {
while (true) {
int partOfYear = random.nextInt(10);
handleDate.verifyNotThreadSafe("201" + partOfYear + "1115040613555");
}
});
Thread thread3 = new Thread(() -> {
while (true) {
int partOfYear = random.nextInt(10);
handleDate.verifyNotThreadSafe("201" + partOfYear + "1115040613555");
}
});
Thread thread4 = new Thread(() -> {
while (true) {
int partOfYear = random.nextInt(10);
handleDate.verifyNotThreadSafe("201" + partOfYear + "1115040613555");
}
});
Thread thread5 = new Thread(() -> {
while (true) {
int partOfYear = random.nextInt(10);
handleDate.verifyNotThreadSafe("201" + partOfYear + "1115040613555");
}
});
Thread thread6 = new Thread(() -> {
while (true) {
int partOfYear = random.nextInt(10);
handleDate.verifyNotThreadSafe("201" + partOfYear + "1115040613555");
}
});
Thread thread7 = new Thread(() -> {
while (true) {
int partOfYear = random.nextInt(10);
handleDate.verifyNotThreadSafe("201" + partOfYear + "1115040613555");
}
});
Thread thread8 = new Thread(() -> {
while (true) {
int partOfYear = random.nextInt(10);
handleDate.verifyNotThreadSafe("201" + partOfYear + "1115040613555");
}
});
Thread thread9 = new Thread(() -> {
while (true) {
int partOfYear = random.nextInt(10);
handleDate.verifyNotThreadSafe("201" + partOfYear + "1115040613555");
}
});
Thread thread10 = new Thread(() -> {
while (true) {
int partOfYear = random.nextInt(10);
handleDate.verifyNotThreadSafe("201" + partOfYear + "1115040613555");
}
});
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
thread6.start();
thread7.start();
thread8.start();
thread9.start();
thread10.start();
}
}
public class HandleDate {
// this is not thread safe
private SimpleDateFormat simpleDateFormatTimestampJPOS = new SimpleDateFormat("yyyyMMddHHmmssSSS");
public void verifyNotThreadSafe(String timeStamp) {
try {
// this is thread safe
//SimpleDateFormat simpleDateFormatTimestampJPOS = new SimpleDateFormat("yyyyMMddHHmmssSSS");
Date date = simpleDateFormatTimestampJPOS.parse(timeStamp);
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
String expectedYear = timeStamp.substring(0, 4);
String actualYear = String.valueOf(localDateTime.getYear());
if (!expectedYear.equals(actualYear)) {
System.out.println("expected:" + expectedYear + ", but real:" + actualYear);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Inconsistent date parsing using SimpleDateFormat
I think you want to use the HH
format, rather than 'hh' so that you are using hours between 00-23. 'hh' takes the format in 12 hour increments, and so it assumes it is in the AM.
So this
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse("2009-08-19 12:00:00");
System.out.print(date.toString());
Should print out
Wed Aug 19 12:00:00 EDT 2009
Android SimpleDateFormat.format giving wrong digit amount, like Minute 0040 randomly
An alternative to this answer is to wrap the formatter in a ThreadLocal
as so:
private static final ThreadLocal<DateFormat> LOGDATEFULL = new ThreadLocal<>() {
@Override protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
}
}
Then you would access it by calling .get().format()
instead.
This lets you avoid the synchronization overhead of coordinating access to a single instance, without worrying about creating separate copies for separate threads manually.
Related Topics
Android - How to Create Clickable Listview
Automatically Log Android Lifecycle Events Using Activitylifecyclecallbacks
How to Move Firebase Child from One Node to Another in Android
Adding Header to All Request with Retrofit 2
Android Webview Always Returns Null for JavaScript Getelementbyid on Loadurl
Can't Install Rjava on Ubuntu System
Aes Cbc Pkcs5Padding Java to Ruby
Why Would One Declare an Immutable Class Final in Java
How to Get a String Between Two Characters
Google Sign in Signed APK Not Working
Add Object to Arraylist at Specified Index
How to Override Action Bar Back Button in Android
How to Load a Jni .Dylib File with a Dependency Without Getting an Unsatifiedlinkerror