simpledateformat

Java day of the week from string

一个人想着一个人 提交于 2019-12-07 05:02:37
问题 I have this simple code: SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date = format.parse("2011-10-29"); calendar.setTime(date); Log.d("Debug","Day of the week = "+(calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY)); The 29th of October is a Saturday so why do I get false? 回答1: Here is an example of how this could happen... SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = format.parse("2011-10-29"); } catch (ParseException

SimpleDateFormat always returns 1970.01.17 with wrong timezone

点点圈 提交于 2019-12-07 04:03:56
问题 I have been using Processing 3.0, and I am trying to print a simple timestamp when my Arduino outputs certain values, but it is not working. I tried to use SimpleDateFormat, but it always returns 1970.01.17 17:48:35 GMT , rather than the actual time. Below is the MVCE: void setup () { SimpleDateFormat format = new SimpleDateFormat ("yyyy.MM.dd HH:mm:ss z"); format.setTimeZone (TimeZone.getDefault()); long timestamp = getTimeNow(); println(format.format(new Date(timestamp))); println(timestamp

SimpleDateFormat takes too long when the time zone is included

天大地大妈咪最大 提交于 2019-12-07 00:35:06
问题 I am using this simple date format SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS Z"); the problem is when I use this it takes too long to convert the time, in logcat I see something like this I/Resources( 4284): Loaded time zone names for en in 272ms. I/Resources( 4284): Loaded time zone names for en in 194ms. I/Resources( 4284): Loaded time zone names for en in 112ms. I/Resources( 4284): Loaded time zone names for en in 111ms. I/Resources( 4284): Loaded time zone names

Difference in hours of two Calendar objects

蹲街弑〆低调 提交于 2019-12-06 19:52:25
问题 I have two Calendar objects, and I want to check what is the difference between them, in hours. Here is the first Calendar Calendar c1 = Calendar.getInstance(); And the second Calendar Calendar c2 = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH); c2.setTime(sdf.parse("Sun Feb 22 20:00:00 CET 2015")); Now lets say that c1.getTime() is: Fri Feb 20 20:00:00 CET 2015 and c2.getTime() is Sun Feb 22 20:00:00 CET 2015 . So is there

Excel日期转Java日期(2019#9#25 11:15:39 转 2019-09-25 11:15:39)

ぐ巨炮叔叔 提交于 2019-12-06 19:03:54
//Excel日期转Java日期(2019#9#25 11:15:39 转 2019-09-25 11:15:39) private Date changeDate(String createdDateStr) { if(StringUtils.isEmpty(createdDateStr)){ return null; } SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd"); Date createdDateReturn = null; String dateStrTemp = ""; if(createdDateStr.indexOf(" ") >= 0){//日前+时间 String[] aa = createdDateStr.split(" "); String dateStr = aa[0]; String timeStr = aa[1]; String[] bb = dateStr.split("#"); dateStrTemp += bb[0] + "-";//yyyy if(bb[1].length() < 2){ dateStrTemp += "0" + bb[1] +

java.text.ParseException: Unparseable date: "2019-11-11"

馋奶兔 提交于 2019-12-06 19:03:40
报错提示 java.text.ParseException: Unparseable date: "2019-11-11" 原来源代码 返回的字符串是:2019-11-11,在解析date的时候一直报错。原来的错误源代码: logger.info("invoiceDate开票日期:"+invoiceOrder.getString("invoiceDate"));//开票日期,格式:YYYY-MM-DD SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date create_dte = df.parse(invoiceOrder.getString("invoiceDate")); 后来发现这个原因: 返回的字符串是:2019-11-11,但是我解析的格式是:yyyy-MM-dd HH:mm:ss导致错误。 输入的字符串要和你想要的格式长度都一样才行 。后来更改代码变为“yyyy-MM-dd” 即可成功执行不报错。 更正代码 logger.info("invoiceDate开票日期:"+invoiceOrder.getString("invoiceDate"));//开票日期,格式:YYYY-MM-DD SimpleDateFormat df = new SimpleDateFormat("yyyy

SimpleDateFormat without the Timezone Offset in Java (GMT+00:00) for Custom Timezone

社会主义新天地 提交于 2019-12-06 18:47:53
问题 Is it possible to format a date time in Java using the SimpleDateFormat class to give the timezone part of a date without having the +0000 after it. Edit We are changing the Default Timezone within Java as follows: SimpleTimeZone tz = new SimpleTimeZone(0, "Out Timezone"); TimeZone.setDefault(tz); Unfortunately, I am in no position to remove this code. I would hazard a guess at the whole system stopping working. I think the initial author put this in to work around some day light saving

How to control SimpleDateFormat parse to 19xx or 20xx?

此生再无相见时 提交于 2019-12-06 16:47:43
问题 Is there a way to parse the following date string as July 23 1916 rather than July 23 2016 ? System.out.println(new SimpleDateFormat("yy/MM/dd", Locale.US).parse("16/07/23")); 回答1: The Java Doc (http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) says: For parsing with the abbreviated year pattern ("y" or "yy"), SimpleDateFormat must interpret the abbreviated year relative to some century. It does this by adjusting dates to be within 80 years before and 20 years after

java.text.ParseException: Unparseable date: “Wed Jan 11 00:00:00 CET 2012”

倖福魔咒の 提交于 2019-12-06 16:10:49
I have the next problem with this date: java.text.ParseException: Unparseable date: "Wed Jan 11 00:00:00 CET 2012" I have this: DateFormat formatter ; Date dateIn=null; formatter = new SimpleDateFormat( "EEE MMM dd HH:mm:ss yyyy" ); try { dateIn = (Date)formatter.parse(dateI); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } What I'm doing bad?. Thanks Use timezone and also a locale SimpleDateFormat( "EEE MMM dd HH:mm:ss z yyyy", Locale.US); to reflect English language in the input string (days and month names). You need to add z in your format string for

Whats the difference in using a and aaa in SimpleDateFormat

时光毁灭记忆、已成空白 提交于 2019-12-06 15:42:40
I want to display current date as 00:50:32 A Here is my code Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss a"); String time = sdf.format(date); System.out.println("time : " + time); But it print as: time : 00:50:32 AM I tried both HH:mm:ss a and HH:mm:ss aaa , but results are the same. Can't do! If the pattern is 4 letters or less, short format is used. So 'a', 'aa', 'aaa' and 'aaaa' are identical. All you can do is to format it without 'a', and add 'A' or 'P' manually. Having said that, using 'HH' (24-hours clock) why would you need AM/PM? I believe for am/pm