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

Java 如何使用日历将日期字符串转换为不同的格式,java,date,time,calendar,Java,Date,Time,Calendar,我正在从弃用的日期库更改为日历,我需要比较不同的时间字符串对象 如何使用日历将时间字符串(xx:xx:xx)转换为完整日期(CET 2015年2月3日星期二21:02:25) 我试过: import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; public class Test2 { public static void main(String[] ar

我正在从弃用的
日期
库更改为
日历
,我需要比较不同的时间字符串对象

如何使用
日历将时间字符串
(xx:xx:xx)
转换为完整日期
(CET 2015年2月3日星期二21:02:25)

我试过:

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

    }

}

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


我希望能够将结果与其他“完整日期对象”进行比较,如2015年CET标准时间2月3日星期二21:02:25

// 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());
输出:

// 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));
您还可以在一行中设置时间

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));
使用

从当前时间构建日期时间

DateTime dt = new DateTime()
            .withMillisOfSecond(0)
            .withHourOfDay(21)
            .withMinuteOfHour(20)
            .withSecondOfMinute(0);
Date date = dt.toDate()
从模式构建日期时间

DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(<YOUR_PATTERN>);
DateTime dt2 = dateTimeFormatter.parseDateTime(<YOUR_DATE_IN_A_STRING>);
从日期时间获取日期

DateTime dt = new DateTime()
            .withMillisOfSecond(0)
            .withHourOfDay(21)
            .withMinuteOfHour(20)
            .withSecondOfMinute(0);
Date date = dt.toDate()
LocalDateTime()在这里帮助了我:

import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class Test {

    public static void main(String[] args) throws ParseException {
        String time = "14:12:01";
        String[] timePieces= time.split(":");
        int H = Integer.parseInt(timePieces[0]);
        int m = Integer.parseInt(timePieces[1]);
        int s = Integer.parseInt(timePieces[2]);

        LocalDateTime today = LocalDateTime.of(LocalDate.now(), LocalTime.of(H, m, s));
        System.out.println(today);

        LocalDateTime today2 = LocalDateTime.now().withHour(H).withMinute(m).withSecond(s).withNano(0);
        System.out.println(today2);
    }

}
要将其转换为“日期”对象,请执行以下操作:


这可能会被认为是粗鲁的,但是。。。您是否尝试过在调用的
set
方法上读取Javadoc?您将如何准确地比较一天中的某个时间和某个日期?String stringDate=“04:10:13”;结果是:“设定时间后的日期:2015年CET时间2月3日星期二16:10:13”,但应该是:“设定时间后的日期:2015年CET时间2月3日星期二04:10:13”啊,这很有效:)!谢谢你:)!我已经接受了你的答案,不能投票,因为我也知道。但是,如果我得到遗漏的要点,我会这样做:)。非常感谢!我建议指定时区()。否则,您的DateTime将被分配JVM的当前默认时区。因此,您的结果将因其运行在哪台计算机上、JVM启动时计算机的时区设置或JVM中的其他Java代码调用而有所不同。。。这确实激励我去帮助其他用户。。。最好不要评论,为什么你不喜欢它。。。态度很好。你被否决了,因为你问了一个关于如何使用日历的问题,但是你的答案没有回答你原来的问题。哦,我想我可以把2标记为正确的…^^改回你的,你是对的:)。
Date date = Date.from(today.atZone(ZoneId.systemDefault()).toInstant());