简体   繁体   English

从Java中的文本文件读取时间

[英]read time from text file in java

I am trying to write some codes in java to read a text file, but when it read a time in (hh:mm) formate as example below: 我试图在Java中编写一些代码来读取文本文件,但是当它在(hh:mm)formate中读取时间时,如下所示:

6:30 14:30

and there is my java code to convert the string to Data formate 还有我的Java代码将字符串转换为Data formate

DateFormat formatter = new SimpleDateFormat("hh:mm");
strLine = br.readLine();
strLine = strLine.trim();
tokens2 = strLine.split(" ");
System.out.println((Date)formatter.parse(tokens2[0]));

the output will be: 输出将是:

Thu Jan 01 06:30:00 AST 1970

it will give in long formate Date , While I want just take timezone(06:30). 它会给你长久的约会Date,而我只想取时区(06:30)。 what should I do ? 我该怎么办 ? Any advice. 任何建议。

edit: the problem solved by Joda time by this code: 编辑:乔达时间通过以下代码解决的问题:

LocalTime.parse(tokens2[0]);

it is just take time 只是需要时间

Firstly, your time pattern is wrong - you should be using HH:mm as you're using a 24-hour clock in your examples. 首先,您的时间模式是错误的-在示例中,您应该使用HH:mm因为您使用的是24小时制。

Secondly, you're parsing a Date . 其次,您正在解析一个Date A Date is just an instant in time: it doesn't know about the time zone, calendar, whether you're interested in just the time, just the date etc. Date只是时间上的一个瞬间:它不知道时区,日历,您是否只对时间,日期等感兴趣。

Thirdly, you're seeing that output because you're basically just calling Date.toString() . 第三,您看到的是输出,因为您基本上只是在调用Date.toString()

There's no type in Java to represent just "a time of day" (which isn't at all the same as a time zone, by the way). Java中没有类型可以代表“一天中的某个时间”(顺便说一句,它与时区完全不同)。 You could format a Date to just include the time part, but if you want to do that, I'd strongly recommend using a time zone of UTC to avoid any possible DST problems. 您可以将Date 格式化为只包含时间部分,但是如果您要这样做,我强烈建议您使用UTC时区,以避免任何可能的DST问题。

A better solution would be to use Joda Time which has a LocalTime type - ideal for your situation. 更好的解决方案是使用具有LocalTime类型的Joda LocalTime非常适合您的情况。 You can parse that ( DateTimeFormatter.parseLocalTime ) and then work with it appropriately, formatting it (again to just a time) whenever you want. 您可以解析该内容( DateTimeFormatter.parseLocalTime ),然后对其进行适当的处​​理,并在需要时对其进行格式化(同样只是一次)。

I assume you want to read and parse 6:30 and output it as 06:30 , right? 我假设您想阅读和解析6:30并将其输出为06:30 ,对吧?

Well, a first step would be to use the formatter, to output the date again: 好吧,第一步是使用格式化程序,再次输出日期:

System.out.println(formatter.format((Date)formatter.parse(tokens2[0])));

Btw, your format would not be able to correctly format 14:30 since h means the hour in am/pm, eg 1-12. 顺便说一句,您的格式将无法正确格式化14:30因为h表示上午/下午的小时数,例如1-12。 Your format should be HH:mm instead, otherwise you'd get the output 02:30 instead of 14:30 . 您的格式应为HH:mm ,否则将得到输出02:30而不是14:30

You can use a SimpledateFormatter in two ways. 您可以通过两种方式使用SimpledateFormatter

  1. to parse a String and create a Date 解析一个字符串并创建一个日期
  2. to create a String aout of a Date 在日期之外创建一个字符串

You want both in your example. 您想在示例中同时使用它们。 First you want to parse a String and create a Date (even if you are only interestet in the time part). 首先,您想解析一个String并创建一个Date(即使您只是时间部分的兴趣爱好)。 In the second step you want to create a String out of this Date and print it to System.out. 在第二步中,您要根据此日期创建一个String并将其打印到System.out。

Date date=formatter.parse(tokens2[0]);     //use the 'formatter' to parse the String
Sytem.out.println(formatter.format(date)); //use the 'formatter' to define the format of the output.

Using java.time 使用java.time

The other answers are correct but now outdated. 其他答案是正确的,但现在已经过时了。

The java.time.LocalTime class represents a time-of-day without a date and without a time zone. java.time.LocalTime类表示没有日期和时区的一天中的时间。 The class can directly parse a string in standard ISO 8601 format using 24-hour clock: HH:MM or HH:MM:SS 该类可以使用24小时制直接解析标准ISO 8601格式的字符串:HH:MM或HH:MM:SS

LocalTime lt = LocalTime.parse( "14:30" );
String output = lt.toString();  // 14:30

Formatting 格式化

Use a DateTimeFormatter if you want other formats. 如果需要其他格式,请使用DateTimeFormatter Even localize automatically. 甚至自动本地化。

DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL );
f = f.withLocale( Locale.CANADA_FRENCH ); // Cultural norms & human language
String output = lt.format( f );

About java.time 关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the old troublesome date-time classes such as java.util.Date , .Calendar , & java.text.SimpleDateFormat . 这些类取代了麻烦的旧日期时间类,例如java.util.Date.Calendarjava.text.SimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to java.time. 现在处于维护模式Joda-Time项目建议迁移到java.time。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参见Oracle教程 And search Stack Overflow for many examples and explanations. 并在Stack Overflow中搜索许多示例和说明。

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP . 多的java.time功能后移植到Java 6和7在ThreeTen-反向移植并且还适于使用AndroidThreeTenABP

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目为将来可能在java.time中添加内容提供了一个试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more. 您可能会在这里找到一些有用的类,例如IntervalYearWeekYearQuarter等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM