为了显示可重现的场景,我正在做以下事情
获取当前系统时间(当地时间)
将本地时间转换为UTC //在这里工作正常
反转UTC时间,返回当地时间.遵循3种不同的方法(如下所列),但所有3种方法仅保留UTC时间.
{
long ts = System.currentTimeMillis();
Date localTime = new Date(ts);
String format = "yyyy/MM/dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat (format);
// Convert Local Time to UTC (Works Fine)
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date gmtTime = new Date(sdf.format(localTime));
System.out.println("Local:" + localTime.toString() + "," + localTime.getTime() + " --> UTC time:" + gmtTime.toString() + "-" + gmtTime.getTime());
// Reverse Convert UTC Time to Locale time (Doesn't work) Approach 1
sdf.setTimeZone(TimeZone.getDefault());
localTime = new Date(sdf.format(gmtTime));
System.out.println("Local:" + localTime.toString() + "," …Run Code Online (Sandbox Code Playgroud)java utc simpledateformat date-formatting timestamp-with-timezone
因此,我一直在思考这个(应该是)简单的练习,让程序将日期字符串转换为GregorianCalendar对象,格式化它,并在完成时将其作为字符串再次返回.
这是一个程序的最后一点,从文件中获取文本,将其分解为单个记录,然后将记录分成单独的数据并将它们分配给一个人对象.
我已经在多个地方检查了代码,代码确实完成了它应该做的事情,直到我调用格式函数,这会抛出IllegalArgumentException.GergorianCalendar对象被赋予它应该被分配的值(虽然打印它是另一个完整的故事,如下所示......),但格式不接受格式化的对象.
不幸的是,教练不太确定如何使用GregorianCalendar和SimpleDateFormat(但指定我们使用它们)并且说"Just Google it"......我试过了,我发现没有任何帮助.
我到目前为止的代码是:
public class DateUtil {
public static GregorianCalendar convertFromDMY(String dd_mm_yy) throws ParseException{
// this actually works, got rid of the original code idea
String[] splitDate = dd_mm_yy.split("-");
int days = Integer.parseInt(splitDate[0]);
int month = Integer.parseInt(splitDate[1]);
int year = Integer.parseInt(splitDate[2]);
// Dates are going in right, checked in print statement,
// but the object is not getting formatted…
GregorianCalendar dateConverted = new GregorianCalendar(year, month, days);
format(dateConverted);
return dateConverted;
}
public static String format(GregorianCalendar date){
SimpleDateFormat …Run Code Online (Sandbox Code Playgroud) java gregorian-calendar simpledateformat illegalargumentexception
我有一个SimpleDateFormat对象,我从一些国际化实用程序检索.解析日期都很好,但我希望能够向我的用户显示格式提示,如"MM/dd/yyyy".有没有办法从SimpleDateFormat对象获取格式模式?
背景:
在我的数据库表中,我有两个时间戳
timeStamp1 = 2011-08-23 14:57:26.662
timeStamp2 = 2011-08-23 14:57:26.9
Run Code Online (Sandbox Code Playgroud)
当我执行"ORDER BY TIMESTAMP ASC"时,timeStamp2被认为是更大的时间戳(这是正确的).
要求:我需要得到这些时间戳的差异(timeStamp2 - timeStamp1)
我的实施:
public static String timeDifference(String now, String prev) {
try {
final Date currentParsed = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(now);
final Date previousParsed = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(prev);
long difference = currentParsed.getTime() - previousParsed.getTime();
return "" + difference;
} catch (ParseException e) {
return "Unknown";
}
}
Run Code Online (Sandbox Code Playgroud)
答案应该是238毫秒,但返回的值是-653毫秒.我不确定我做错了什么.有什么建议?
我需要担心这个警告吗?如果我忽略警告怎么办?
这是什么警告表示:
为了让本地格式使用getDateInstance(),getDateTimeInstance()或者getTimeInstance(),也可以使用new SimpleDateFormat(String template, Locale locale)具有例如Locale.US用于ASCII日期.
在下面的代码的第二行.该应用程序与代码正常工作.我想显示日期,例如19 Nov 2014.
public static String getFormattedDate(long calendarTimeInMilliseconds) {
SimpleDateFormat sdfDate = new SimpleDateFormat("d MMM yyyy"); //ON THIS LINE
Date now = new Date();
now.setTime(calendarTimeInMilliseconds);
String strDate = sdfDate.format(now);
return strDate;
}
Run Code Online (Sandbox Code Playgroud)
我觉得这是如图所示日期格式以正确的方式在这里.
我需要将包含日期的String转换为日期对象.字符串的格式为"yyyy-mm-dd HH:mm:ss.SSSSSS",我想在日期对象中使用相同的格式.
例如,我有一个字符串"2012-07-10 14:58:00.000000",我需要生成的日期对象具有相同的格式.
我已经尝试了以下方法,但结果并不像预期的那样.
java.util.Date temp = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss.SSSSSS").parse("2012-07-10 14:58:00.000000");
DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
Date thisDate = dateFormat.parse("2012-07-10 14:58:00.000000");
Run Code Online (Sandbox Code Playgroud)
结果是"Tue Jan 10 14:58:00 EST 2012".请让我知道我哪里出错了.
谢谢,Yeshwanth Kota
有没有办法解析以下日期字符串July 23 1916而不是July 23 2016?
System.out.println(new SimpleDateFormat("yy/MM/dd", Locale.US).parse("16/07/23"));
Run Code Online (Sandbox Code Playgroud) 我有这个代码:
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
dateFormat.setLenient(false);
Date date = dateFormat.parse("10/20/20128");
Run Code Online (Sandbox Code Playgroud)
我希望dateFormat.parse调用抛出ParseException,因为我提供的年份是5个字符而不是4个,就像我定义的格式一样.但由于某些原因,即使将lenient设置为false,此调用也会返回Date对象10/20/20128.
这是为什么?这对我来说没什么意义.是否有其他设置使其更严格?
这是在RHEL上的Java 7(51)上有24个内核我们注意到,当我们增加线程池大小时,包含在本地线程中的java SimpleDateFormat的平均响应时间会增加.这是预期的吗?或者,我只是在做一些愚蠢的事情?

测试程序
public class DateFormatterLoadTest {
private static final Logger LOG = Logger.getLogger(DateFormatterLoadTest .class);
private final static int CONCURRENCY = 10;
public static void main(String[] args) throws Exception {
final AtomicLong total = new AtomicLong(0);
ExecutorService es = Executors.newFixedThreadPool(CONCURRENCY);
final CountDownLatch cdl = new CountDownLatch(CONCURRENCY);
for (int i = 0; i < CONCURRENCY; i++) {
es.execute(new Runnable() {
@Override
public void run() {
try {
int size = 65000;
Date d = new Date();
long time = System.currentTimeMillis(); …Run Code Online (Sandbox Code Playgroud) 根据SimpleDateFormat类文档,Java它的日期模式不支持超过毫秒的时间粒度.
所以,日期字符串就像
通过模式解析时
实际上将.符号后面的整数解释为(接近10亿!)毫秒,而不是纳秒,导致日期
即超过11天.令人惊讶的是,使用较少数量的S符号仍会导致所有9位数被解析(而不是,例如,最左边的3位.SSS).
有两种方法可以正确处理此问题:
是否有任何其他方法可以通过向标准SimpleDateFormat实现提供模式来获得正确的解决方案,而无需任何其他代码修改或字符串操作?
java ×10
simpledateformat ×10
date ×3
android ×1
concurrency ×1
date-format ×1
date-parsing ×1
milliseconds ×1
performance ×1
string ×1
thread-local ×1
utc ×1