java - 如何使用日历将日期字符串转换为不同格式

标签 java date time calendar

我正在从已弃用的 Date 库更改为 Calendar,我需要比较不同的时间字符串对象。

如何使用 将时间字符串 (xx:xx:xx) 转换为完整日期 (Tue Feb 03 21:02:25 CET 2015) >日历

我尝试过:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Test2 {

    public static void main(String[] args) throws ParseException {
        // Date date = new SimpleDateFormat("HH:mm:ss").parse("21:20:00"); // Date ist 1970!
        // System.out.println(date.toString());

        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss | dd.MM.yy");
        cal.set(cal.YEAR, cal.MONTH, cal.DATE, 21, 20, 00);
        System.out.println(sdf2.format(cal.getTime()));

    }

}

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Test2 {

    public static void main(String[] args) throws ParseException {
        // Date date = new SimpleDateFormat("HH:mm:ss").parse("21:20:00"); // Date ist 1970!
        // System.out.println(date.toString());

        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss | dd.MM.yy");
        cal.setTime(sdf.parse("21:20:00"));
        cal.set(Calendar.YEAR, Calendar.MONTH, Calendar.DATE);
        System.out.println(sdf2.format(cal.getTime()));

    }

}

但他们分别返回:21:20:00 | 05.03.01

我希望能够将结果与其他“完整日期对象”进行比较,例如 Tue Feb 03 21:02:25 CET 2015

最佳答案

更新:

// Set the time in String
String stringDate = "04:10:13";
// Parse this time 
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Calendar cal = Calendar.getInstance();
// Set the parsed time to a Calendar object
cal.setTime(sdf.parse(stringDate));

现在您必须获取今天的日期并使用 Calendar#set 方法将上述时间设置为该日期。

// get today's time
Calendar today = Calendar.getInstance();
// print today date with current time,
System.out.println("Date before time is set: " + today.getTime());

// set today's hour to above time
today.set(Calendar.HOUR, cal.get(Calendar.HOUR));
// set today's minute to above time
today.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
// set today's seconds to above time
today.set(Calendar.SECOND, cal.get(Calendar.SECOND));
// print your new time
System.out.println("Date after time is set: " + today.getTime());

输出:

Date before time is set: Thu Feb 05 02:52:55 PKT 2015
Date after time is set: Thu Feb 05 04:10:13 PKT 2015

您还可以在一行中设置时间,

today.set(today.get(Calendar.YEAR), today.get(Calendar.MONTH) , today.get(Calendar.DATE), cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));

关于java - 如何使用日历将日期字符串转换为不同格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28308311/

相关文章:

java - Java虚拟机的启动时间由什么组成?

php - 使用 php 将时间转换为不同的时区

sql - 如何在 PostgreSQL 中转换这些数据

java - 按下按钮时不会重置时间

sql - 使用 PostgreSQL 减去两个日期

perl - 如何使用 perl 对应用程序输出添加时间戳

mysql - 每小时平均 time()

java - 通过 hibernate 的 Joda-Money 持久化

java - 我可以为多个 Web 操作仅调用一次 PageFactory 实例吗?

java - 在 Java 中,如何同时多次播放同一个音频片段?