simpledateformat

SimpleDateFormat returns wrong time zone during parse

早过忘川 提交于 2019-12-17 23:46:22
问题 I am facing a strange problem during the creation of Date object. I have the below code to set the time zone of the date to UTC. Before the parse statement i see that sdfDate has time zone as UTC but after the parse statement i see that punchDate has value: Sat Mar 30 01:00:00 CET 2013, this i believe is coming from my system. But how do i make sure that the time zone remains same even after parse. Date punchDate = null; Date punchTime = null; SimpleDateFormat sdfDate = new SimpleDateFormat(

Optional parts in SimpleDateFormat

非 Y 不嫁゛ 提交于 2019-12-17 18:34:00
问题 I'm reading in date strings that could be with or without a time zone adjustment: yyyyMMddHHmmssz or yyyyMMddHHmmss . When a string is missing a zone, I'll treat it as GMT. I'm not seeing any way to create optional sections in a SimpleDateFormat , but maybe I'm missing something. Is there a way to do this with a SimpleDateFormat , or should I just write a new concrete DateFormat to handle this? 回答1: I would create two SimpleDateFormat, one with a time zone and one without. You can look at the

Converting String to Time using SimpleDateFormat issues

旧时模样 提交于 2019-12-17 17:17:13
问题 So, I'm using SimpleDateFormat to convert a String which is "HH:MM AA" to "HH:MM", The problem is my code works fine on eclipse but when I run this on Android Studio it shows wrong output. Here are my both codes. 回答1: The problem is you are using "MM" as minutes but it should be "mm". "MM" is for months. The part "HH" is fine when you want the 24 hour values back. try soemthing like this: public static String getTimeFormatted(String time){ String s = ""; try{ SimpleDateFormat sdf = new

Simple Date format returns Wrong date intermittently [duplicate]

心已入冬 提交于 2019-12-17 17:15:31
问题 This question already has answers here : Why is Java's SimpleDateFormat not thread-safe? [duplicate] (9 answers) Closed last year . 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

Date object SimpleDateFormat not parsing timestamp string correctly in Java (Android) environment

大城市里の小女人 提交于 2019-12-17 09:53:23
问题 I'm using the SimpleDateFormat object with the Date object as shown below. The problem lis that the Date object shows the wrong date, which is a few minutes off from the original string. The Date object appears to store the time in total milliseconds in the debugger. Any ideas on the problem? import java.text.SimpleDateFormat; import java.util.Date; Date played_at_local; dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSSSSZ"); played_at_local = dateFormat.parse("2011-04-11T22:27:18

Date object SimpleDateFormat not parsing timestamp string correctly in Java (Android) environment

浪子不回头ぞ 提交于 2019-12-17 09:53:02
问题 I'm using the SimpleDateFormat object with the Date object as shown below. The problem lis that the Date object shows the wrong date, which is a few minutes off from the original string. The Date object appears to store the time in total milliseconds in the debugger. Any ideas on the problem? import java.text.SimpleDateFormat; import java.util.Date; Date played_at_local; dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSSSSZ"); played_at_local = dateFormat.parse("2011-04-11T22:27:18

Java SimpleDateFormat pattern for W3C XML dates with timezone [duplicate]

扶醉桌前 提交于 2019-12-17 07:26:33
问题 This question already has answers here : Java Time Zone When Parsing DateFormat (7 answers) Closed 3 years ago . I am trying to parse a W3C XML Schema date like the following "2012-05-15T07:08:09+03:00" which complies with the ISO 8601 version of the W3C XML Schema dateTime specification. In the above date, the timezone identifier is "+03:00" , but no SimpleDateFormat pattern apparently exists to represent it. If the timezone were "+0300" , then Z (uppercase) would be applicable and the

Java SimpleDateFormat pattern for W3C XML dates with timezone [duplicate]

给你一囗甜甜゛ 提交于 2019-12-17 07:26:26
问题 This question already has answers here : Java Time Zone When Parsing DateFormat (7 answers) Closed 3 years ago . I am trying to parse a W3C XML Schema date like the following "2012-05-15T07:08:09+03:00" which complies with the ISO 8601 version of the W3C XML Schema dateTime specification. In the above date, the timezone identifier is "+03:00" , but no SimpleDateFormat pattern apparently exists to represent it. If the timezone were "+0300" , then Z (uppercase) would be applicable and the

SimpleDateFormat parse(string str) doesn't throw an exception when str = 2011/12/12aaaaaaaaa?

我是研究僧i 提交于 2019-12-17 04:34:22
问题 Here is an example: public MyDate() throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d"); sdf.setLenient(false); String t1 = "2011/12/12aaa"; System.out.println(sdf.parse(t1)); } 2011/12/12aaa is not a valid date string. However the function prints "Mon Dec 12 00:00:00 PST 2011" and ParseException isn't thrown. Can anyone tell me how to let SimpleDateFormat treat "2011/12/12aaa" as an invalid date string and throw an exception? 回答1: The JavaDoc on parse(...) states

Displaying AM and PM in lower case after date formatting

我们两清 提交于 2019-12-17 04:31:47
问题 After formatting a datetime, the time displays AM or PM in upper case, but I want it in lower case like am or pm. This is my code: public class Timeis { public static void main(String s[]) { long ts = 1022895271767L; String st = null; st = new SimpleDateFormat(" MMM d 'at' hh:mm a").format(ts); System.out.println("time is " + ts); } } 回答1: Unfortunately the standard formatting methods don't let you do that. Nor does Joda. I think you're going to have to process your formatted date by a simple