Reputation: 25
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:
6:30 14:30
and there is my java code to convert the string to 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). 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
Upvotes: 1
Views: 2323
Reputation: 339303
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. The class can directly parse a string in standard ISO 8601 format using 24-hour clock: HH:MM or HH:MM:SS
LocalTime lt = LocalTime.parse( "14:30" );
String output = lt.toString(); // 14:30
Use a DateTimeFormatter
if you want other formats. Even localize automatically.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL );
f = f.withLocale( Locale.CANADA_FRENCH ); // Cultural norms & human language
String output = lt.format( f );
The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Upvotes: 0
Reputation: 88727
I assume you want to read and parse 6:30
and output it as 06:30
, right?
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, e.g. 1-12. Your format should be HH:mm
instead, otherwise you'd get the output 02:30
instead of 14:30
.
Upvotes: 1
Reputation: 10151
You can use a SimpledateFormatter
in two ways.
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). In the second step you want to create a String out of this Date and print it to 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.
Upvotes: 0
Reputation: 1501976
Firstly, your time pattern is wrong - you should be using HH:mm
as you're using a 24-hour clock in your examples.
Secondly, you're parsing a 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.
Thirdly, you're seeing that output because you're basically just calling 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). 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.
A better solution would be to use Joda Time which has a LocalTime
type - ideal for your situation. You can parse that (DateTimeFormatter.parseLocalTime
) and then work with it appropriately, formatting it (again to just a time) whenever you want.
Upvotes: 5