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:
DateFormat formatter = new SimpleDateFormat("hh:mm");
strLine = br.readLine();
strLine = strLine.trim();
tokens2 = strLine.split(" ");
Thu Jan 01 06:30:00 AST 1970
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 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
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.