simpledateformat

如何测试这个方法--性能篇

你离开我真会死。 提交于 2019-12-08 14:18:24
本文题目来自于知识星球,后台回复“知识星球”可参与问答。 书接上回,继续说说这个生成唯一订单号的方法,这次来讲讲性能问题。 先贴原始代码: /** * 生产唯一的交易订单号 * * @return */ public static String createUniqueOrderNo() { SimpleDateFormat nyrsfm = new SimpleDateFormat("yyyyMMddHHmmss"); return nyrsfm.format(new Date()) + getRandomLengthCode(4); } /** * 获取随机的短信验证码 * * @return */ public static String getRandomLengthCode(int length) { return String.valueOf((int) ((Math.random() * 9 + 1) * Math.pow(10, length - 1))); } 首先来看第一个方法,第一眼看过去,发现一个问题:该类是一个工具类,方法都是静态的。在第一个方法中,SimpleDateFormat对象每次调用的时候都会创建,而且是一样的对象,这里既浪费内存又浪费CPU,总体来讲浪费时间。 修改如下: static SimpleDateFormat nyrsfm = new

Android Chronometer: time format issue

99封情书 提交于 2019-12-08 12:53:02
问题 Why my chronometer wouldn't stop after I've told it to? I have found out that it seems to be isolated to the tablet I have tested it on (7 inch Galaxy Tab ). I have no idea why it would work on my phone yet not work on the tablet. The Chronometer is set up the same in both layouts (since I am using different layouts for the different screen sizes). Here is how the Chronometer is handled in the class: this.chrono = (Chronometer) findViewById(R.id.calling_crono); chrono

grails date format in English language

落爺英雄遲暮 提交于 2019-12-08 10:24:47
问题 I have the following code to format the date: def currentDate = new Date().format('E, dd MMM yyyy') The format is as I expected, however it is written in the language of my computer. How can I get it to give the date in English? Any help? Thanks! 回答1: If you're running in context of a Controller I would suggest you use def currentDate = new Date() def formattedDate = g.formatDate(date:currentDate, format: 'E, dd MMM yyyy') 回答2: You can use standard date formatter: import java.text.* import

Cannot convert my String to Date

冷暖自知 提交于 2019-12-08 05:53:52
问题 i was searching how to convert a string to a date, so i've found some examples on stacko. . So i used SimpleDateFormat and tried to parse but my compiler (Gradle from AndroidStudio) send me this error : Unhandled exception : java.text.ParseException. There is my code : public static int compareDate(String sdate1, String sdate2) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd", Locale.FRANCE); Date date1 = simpleDateFormat.parse(sdate1); // there is the error [...] } Why

TimeZone broken in SimpleDateFormat in android 2.3

我们两清 提交于 2019-12-08 04:30:21
问题 Android 2.3 was recently released last night. So naturally I tried my app on it and found there was date formatting issue. I have noticed the DateFormatter produces different formats. So do this in a simple Java program: ((SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG)).format(new Date()); Output is December 7, 2010 11:49:40 AM EST Do the same thing in an android emulator and you get December 7, 2010 11:42:50 AM GMT-05:00 Notice the different time zone. Has

Convert java.time.LocalDate to java.util.Date

情到浓时终转凉″ 提交于 2019-12-08 03:02:17
问题 I have java.time.LocalDate Object in yyyy-MM-dd format. I would like to know how to convert this to java.util.Date with MM-dd-yyyy format. getStartDate() method should be able to return Date type object with the format MM-dd-yyyy. DateParser class package com.accenture.javadojo.orgchart; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.util.Locale; public class DateParser { public static LocalDate parseDate

java 获取指定天数之前的日期

泄露秘密 提交于 2019-12-07 22:46:21
目的:获取当前时间前n天的日期 (示例就以30天为例) 运行代码: 返回的类型 java.sql.Date long time = 30*86400000; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); String format = simpleDateFormat.format(new Date(System.currentTimeMillis() - time)); System.out.println("获取到三十天前的日期为:"+java.sql.Date.valueOf(format)); 粗看代码是没有问题,运行出来的时间竟然是: 今天是5月30日,三十天前是6月19日。。。 debug断点。。。 int的取值范围 为: -2^31——2^31-1,即-2147483648——2147483647 。。。 强转啊。。数据类型 30天之前的时间是259.。。。而int的取值范围是214.。。。。强转成负数。。而用long类型的值就不会。。 记一个坑。。 附录: java计算当前时间指定天数之前的日期,指定时间日期指定天数之前的日期。。的工具类 //方法一 long time = (long)30*86400000; SimpleDateFormat

String Date Calendar之间的转换

99封情书 提交于 2019-12-07 13:30:48
1.Calendar 转化 String Calendar calendat = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String dateStr = sdf.format(calendar.getTime()); 2.String 转化 Calendar String str="2012-5-27"; SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd"); Date date =sdf.parse(str); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); 3.Date 转化 String SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd"); String dateStr=sdf.format(new Date()); 4.String 转化 Date String str="2012-5-27"; SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd"); Date date= sdf

Parse short US date into yyyy-MM-dd, java

偶尔善良 提交于 2019-12-07 10:54:34
I want to parse the date "3/27/11" which I think is equal to US short date. DateFormat df1 = new SimpleDateFormat("MM/dd/yy"); DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd"); Date date = (Date) df1.parseObject("03/27/11"); System.out.println("New date: " + df2.format(date)); I found the code above in several java tutorials but it doesn't seem to work. For some how I get, Exception in thread "main" java.lang.AssertionError: Default directory must be absolute/non-UNC Here is what I want to achieve, input: 3/27/11 (03/27/11 should also be a valid input) output: 2011-03-27 Thanks in advance!

Eliminating the subtle whitespace handling difference between DateTimeFormat and Joda's DateTimeFormatter

强颜欢笑 提交于 2019-12-07 08:30:55
问题 We have some existing code like this: DateFormat[] dateFormats = { new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH), new SimpleDateFormat("d MMM yyyy HH:mm:ss Z", Locale.ENGLISH) }; For thread safety reasons, I tried to convert it to use Joda Time's formatters, so: DateTimeFormatter[] dateFormats = { DateTimeFormat.forPattern("EEE, d MMM yyyy HH:mm:ss Z") .withLocale(Locale.ENGLISH) .withOffsetParsed(), DateTimeFormat.forPattern("d MMM yyyy HH:mm:ss Z") .withLocale(Locale