SimpleDateFormat parse loses timezone [duplicate]

All I needed was this : SimpleDateFormat sdf = new SimpleDateFormat(“yyyy.MM.dd HH:mm:ss”); sdf.setTimeZone(TimeZone.getTimeZone(“GMT”)); SimpleDateFormat sdfLocal = new SimpleDateFormat(“yyyy.MM.dd HH:mm:ss”); try { String d = sdf.format(new Date()); System.out.println(d); System.out.println(sdfLocal.parse(d)); } catch (Exception e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } Output : slightly dubious, but I want … Read more

Get yesterday’s date using Date [duplicate]

Update There has been recent improvements in datetime API with JSR-310. Instant now = Instant.now(); Instant yesterday = now.minus(1, ChronoUnit.DAYS); System.out.println(now); System.out.println(yesterday); https://ideone.com/91M1eU Outdated answer You are subtracting the wrong number: Use Calendar instead: private Date yesterday() { final Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); return cal.getTime(); } Then, modify your method to the following: … Read more

12:xx shown as 00:xx in SimpleDateFormat.format(“hh:mm:ss”)

First a couple of formatters like yours, only using DateTimeFormatter from java.time, the modern Java date and time API: private static DateTimeFormatter dtfOld = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”); private static DateTimeFormatter dtfNew = DateTimeFormatter.ofPattern(“dd-MM-yyyy HH:mm:ss”); Two things to note: (1) Declare the formatters in the logical order, the order in which you are going to use them. … Read more

Getting error java.text.ParseException: Unparseable date: (at offset 0) even if the Simple date format and string value are identical

It says that your date-time string is unparseable at index 0. Index 0 is where it says Mon, so the three letter time zone abbreviation is not the first suspect. The locale is. “Mon” works as abbreviation for Monday in English, but not in very many other languages. So if your device has a non-English … Read more

Date format parse exception – “EEE MMM dd HH:mm:ss Z yyyy” [duplicate]

I’m going to assume that Locale.getDefault() for you is pl-PL since you seem to be in Poland. English words in date strings therefore cause an unparseable date. An appropriate Polish date String would be something like “Wt paź 16 00:00:00 -0500 2013” Otherwise, change your Locale to Locale.ENGLISH so that the SimpleDateFormat object can parse … Read more