15

私は文字列 x = "1086073200000" を持っています。これは基本的に、日付に変換する必要があるミリ秒です。

私が使用している変換するには

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())); 

問題は、文字列 x を long に変換すると、 long のサイズの制限により一部の桁がなくなることです。

文字列全体を保持するにはどうすればよいですか。

ありがとう。

4

4 に答える 4

41
double tempo=Double.parseDouble(z);

Stringa であるはずの your を aLongとして解析するのはなぜDoubleですか?

使用してみてください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())); 
于 2012-09-20T00:20:57.507 に答える
8

このコードを試してみましたが、うまくいきました

public static void main(String[] args) {
    String x = "1086073200000";
    long foo = Long.parseLong(x);
    System.out.println(x + "\n" + foo);

    Date date = new Date(foo);
    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    System.out.println(formatter.format(date)); 
}
于 2012-09-20T00:23:25.170 に答える
4
于 2016-06-29T01:24:03.510 に答える
4
Date date = new Date(milliseconds);
于 2014-09-22T05:56:09.627 に答える