将毫秒字符串转换为Java中的日期 [英] Convert millisecond String to Date in Java

查看:82
本文介绍了将毫秒字符串转换为Java中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串x =1086073200000。这基本上是毫秒,我需要转换为一个日期。



要转换我正在使用

  DateFormat formatter = new SimpleDateFormat(dd / MM / yyyy); 

long tempo1 = Long.parseLong(x);
System.out.println(tempo1); //输出是86073200000而不是整个事情
long milliSeconds = 1346482800000L;

日历calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
System.out.println(formatter.format(calendar.getTime()));

问题是当我将字符串x转换为长时,有些数字由于限制



如何保留整个字符串。



THanks。

解决方案


  double tempo = Double.parseDouble(z); 


为什么要解析你的 / code> c $ c> c $ c $? b

尝试使用 Long.parseLong

  
DateFormat formatter = new SimpleDateFormat(dd / MM / yyyy);

long milliSeconds = Long.parseLong(x);
System.out.println(milliSeconds);

日历calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
System.out.println(formatter.format(calendar.getTime()));


i have a String x = "1086073200000" . This is basically millisecond which I need to convert to a Date.

To convert i am using

DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

long tempo1=Long.parseLong(x);
System.out.println(tempo1);  // output is 86073200000 instead of the whole thing
long milliSeconds=1346482800000L;

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
System.out.println(formatter.format(calendar.getTime())); 

The problem is when i convert the string x to long , some digits go away due to the limit on the size of long.

How do I preserve the entire String.

THanks.

解决方案

double tempo=Double.parseDouble(z);

Why are you parsing your String which is supposed to be a Long as a Double?

Try using Long.parseLong:

String x = "1086073200000"

DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

long milliSeconds= Long.parseLong(x);
System.out.println(milliSeconds);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
System.out.println(formatter.format(calendar.getTime())); 

这篇关于将毫秒字符串转换为Java中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆