如何将日期格式的字符串时间戳转换为日期?

huangapple go评论136阅读模式
英文:

How do I convert date formatted string timestamp into date?

问题

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class TimestampConversion {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
        long now = System.currentTimeMillis();
        String toTimeStamp = dateFormatter.format(now);
        long nowMinus1Minutes = now - TimeUnit.MINUTES.toMillis(1);
        String fromTimeStamp = dateFormatter.format(nowMinus1Minutes);
        System.out.println(fromTimeStamp);
        System.out.println(toTimeStamp);

        Date fromDate = new Date(nowMinus1Minutes);
        Date toDate = new Date(now);

        System.out.println(dateFormatter.format(fromDate));
        System.out.println(dateFormatter.format(toDate));
    }
}

Please note that I've provided the translated code as you requested.

英文:

I need to convert the current timestamp and current minus 1-minute timestamp into a given format. I was able to convert the desired format. But it was returning as String.

I need the formatted output as Date instead of String.

public static void main(String[] args) throws ParseException
{
    SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
    long now = System.currentTimeMillis();
    String toTimeStamp = dateFormatter.format(now);
    long nowMinus1Minutes = now - TimeUnit.MINUTES.toMillis(1);
    String fromTimeStamp = dateFormatter.format(nowMinus1Minutes);
    System.out.println(fromTimeStamp);
    System.out.println(toTimeStamp);

    // Tried with parse
    Date fromDate = dateFormatter.parse(fromTimeStamp);
    Date toDate = dateFormatter.parse(toTimeStamp);

    System.out.println(fromDate);
    System.out.println(toDate);

}

Output:(String)

2020-07-24 12:13:00 
2020-07-24 12:12:00

Output: Pose Parsed the string as Date

Fri Jul 24 12:16:00 IST 2020
Fri Jul 24 12:17:00 IST 2020

Expected Output:

2020-07-24 12:13:00 (As Date Object)
2020-07-24 12:12:00 (As Date Object)

答案1

得分: 2

无法可能。 最近理解。 正在关闭此事。

英文:

Can't be possible. Understood lately. Closing this.

答案2

得分: 1

一旦您将字符串转换为日期,无论最初使用的格式是什么,它都会丢失。

如果您想以 "yyyy-MM-dd HH:mm:00" 的格式显示数据,请再次使用您的格式化程序,否则 Date 类将保持其默认格式:

System.out.println(formatter.format(fromDate));
System.out.println(formatter.format(toDate));
英文:

Once you transform your String into a Date, the initial format you used, whatever it is is lost.

If you want to display your data in the "yyyy-MM-dd HH:mm:00", use your formatter again otherwise Date class will stick to its default format:

System.out.println(formatter.format(fromDate));
System.out.println(formatter.format(toDate));

答案3

得分: 1

如果您需要两个日期对象,可以像这样执行:

OffsetDateTime offsetDateTime1 = OffsetDateTime.now();
OffsetDateTime offsetDateTime2 = offsetDateTime1.minusMinutes(1);

System.out.println(Date.from(offsetDateTime1.toInstant()));
System.out.println(Date.from(offsetDateTime2.toInstant()));

如果您需要以其他格式打印时间戳,之后可以在Date对象上使用SimpleDateFormat

英文:

If you need two Date-Object, you could do it like this:

    OffsetDateTime offsetDateTime1 = OffsetDateTime.now();
    OffsetDateTime offsetDateTime2 = offsetDateTime1.minusMinutes(1);

    System.out.println(Date.from(offsetDateTime1.toInstant()));
    System.out.println(Date.from(offsetDateTime2.toInstant()));

If you need the timestamp to be printed in other format, you can use SimpleDateFormat on the Date-Objects afterwards.

huangapple
  • 本文由 发表于 2020年7月24日 14:54:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63068358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定