web-dev-qa-db-ja.com

java)でタイムスタンプ文字列をlongに変換します

DBからタイムスタンプを取得し、時間のみを取得して2つの時間を比較する必要があります。

//以下は文字列値です

 String st1 = "2015-07-24T09:39:14.000Z";      
 String st2 = "2015-07-24T09:45:44.000Z";

//時間のみを取得9:39:14

 String s = st1.substring(st1.indexOf("T") + 1, st1.indexOf(".0"));

//文字列をLongに。

 Long time = Long.parseLong(s);

 Long tim1=Long.valueOf(s).longValue();

エラー:

Java.lang.NumberFormatException.forInputString(Unknown Source)
    at Java.lang.Long.parseLong(Unknown Source)
    at Java.lang.Long.parseLong(Unknown Source)
6
Thanga

もう1つのオプションは、SimpleDateFormatを使用することです(JODA Timeと比較して最適ではない場合があります)

public static void main(String[] args) throws ParseException {
        String st1 = "2015-07-24T09:39:14.000Z";
        String st2 = "2015-07-24T09:45:44.000Z";

        String time1 = st1.substring(st1.indexOf("T") + 1, st1.indexOf(".0"));
        String time2 = st2.substring(st2.indexOf("T") + 1, st2.indexOf(".0"));

        Date dateTime1 = new Java.text.SimpleDateFormat("HH:mm").parse(time1);
        Date dateTime2 = new Java.text.SimpleDateFormat("HH:mm").parse(time2);

        System.out.println(dateTime1.after(dateTime2));

    }
3
TheCodingFrog

この方法を試してください、サンプルコード:

Java.sql.Timestamp ts2 = Java.sql.Timestamp.valueOf("2015-07-24T09:45:44.000Z");
long tsTime2 = ts2.getTime();
9
Bipool

最も簡単な解決策は、Java 8のDate/TimeAPIを使用することです。

LocalDateTime from = LocalDateTime.parse("2015-07-24T09:39:14.000Z", DateTimeFormatter.ISO_DATE_TIME);
LocalDateTime to = LocalDateTime.parse("2015-07-24T09:45:44.000Z", DateTimeFormatter.ISO_DATE_TIME);
System.out.println(from + " - " + to);

LocalTime fromTime = from.toLocalTime();
LocalTime toTime = to.toLocalTime();

System.out.println(fromTime + " - " + toTime);

System.out.println(fromTime + " before " + toTime + " = " + fromTime.isBefore(toTime));
System.out.println(fromTime + " after " + toTime + " = " + fromTime.isAfter(toTime));
System.out.println(fromTime + " equals " + toTime + " = " + fromTime.equals(toTime));
System.out.println(fromTime + " compareTo " + toTime + " = " + fromTime.compareTo(toTime));

どの出力

2015-07-24T09:39:14 - 2015-07-24T09:45:44
09:39:14 - 09:45:44
09:39:14 before 09:45:44 = true
09:39:14 after 09:45:44 = false
09:39:14 equals 09:45:44 = false
09:39:14 compareTo 09:45:44 = -1

Java 8を使用していない場合は、 Joda-Time を使用します。これは同様に機能します

ジョーダタイムの例...

import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;
import org.joda.time.format.ISODateTimeFormat;

public class JodaTimeTest {

    public static void main(String[] args) {
        LocalDateTime from = LocalDateTime.parse("2015-07-24T09:39:14.000Z", ISODateTimeFormat.dateTime());
        LocalDateTime to = LocalDateTime.parse("2015-07-24T09:45:44.000Z", ISODateTimeFormat.dateTime());

        LocalTime fromTime = from.toLocalTime();
        LocalTime toTime = to.toLocalTime();

        System.out.println(fromTime + " - " + toTime);

        System.out.println(fromTime + " before " + toTime + " = " + fromTime.isBefore(toTime));
        System.out.println(fromTime + " after " + toTime + " = " + fromTime.isAfter(toTime));
        System.out.println(fromTime + " equals " + toTime + " = " + fromTime.equals(toTime));
        System.out.println(fromTime + " compareTo " + toTime + " = " + fromTime.compareTo(toTime));
    }

}

どの出力

09:39:14.000 - 09:45:44.000
09:39:14.000 before 09:45:44.000 = true
09:39:14.000 after 09:45:44.000 = false
09:39:14.000 equals 09:45:44.000 = false
09:39:14.000 compareTo 09:45:44.000 = -1
3
MadProgrammer
        String date = "2015-07-24T09:39:14.000Z";
        //Now we are getting the time only from the above string.
        String time = date.substring(12, 19); 
        System.out.println("Time is: "+time);
        //Since we cannot convert symbols like":" to long we are removing them.
        String timeInLong = time.replaceAll(":", "");
        System.out.println("Time in long format : "+Long.parseLong(timeInLong));
1
Salim Malik

次のコードで試すことができます、私はライブラリjoda-timeを使用します

Java.sql.Timestamp ts2 = Java.sql.Timestamp.valueOf("2015-07-24T09:45:44.000Z");long dateLong=new DateTime(ts2).toDate().getTime();

0
jaimeRambo