1

I want to print the Time (hours:minutes:seconds) from a date gotten from a String.

I have a string with the next value:

String dateStr = "Tue, 04 Aug 2015 12:09:10 GMT"

I parse it to a Date:

SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
Date d = format.parse(dateStr);

But when I try to get the hours, minutes and seconds, I get a different value:

System.out.println("The current time is: " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds());

Prints: "The current time is: 9:9:10"

Whats wrong? Shouldn't print "The current time is: 12:9:10"?

Thanks :)


More information:

When I create the Date, it automatically sets the Timezone to GMT-03:00 2015, and that's why is printing 3 hours less. How can I set the timezone?

IIRed-DeathII
  • 1,117
  • 2
  • 15
  • 34
  • Notice all the deprecated methods u are using... Use a Calendar object and set the datetime you´ve read to it and, then, use the `Calendar.get(Calendar.XXXXXXX)` methods: `SimpleDateFormat sdf = new SimpleDateFormat("FORMAT HERE", Locale.EN_US); cal.setTime(sdf.parse("DATE HERE")); System.out.println(cal.get(Calendar.MINUTE));` – eduyayo Aug 04 '15 at 12:36
  • Yes, may be using other than Date can solve the problem. Can you put your complete solution in an answer? – IIRed-DeathII Aug 04 '15 at 12:43
  • have u noticed that the difference is exactly the one u´ve got from Montevideo to GMT? – eduyayo Aug 04 '15 at 12:44
  • Yes I noticed. Whats wrong with my code then? Why am I getting GMT-3? Is because the format? – IIRed-DeathII Aug 04 '15 at 13:08
  • U're printing out an uruguayan date after reading a gmt. The output conversion screws it. Create a calendar like this: `cal = new GregorianCalendar(Locale.ENGLISH, TimeZone.forName("GMT"));` and set the time. It will work. Btw the forname may not be accurate because I'm not with my computer – eduyayo Aug 04 '15 at 16:43

4 Answers4

2

try this, you will get it

try {
        String dateStr = "Tue, 04 Aug 2015 12:09:10 GMT";

        SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss",Locale.getDefault());

        SimpleDateFormat formatTarget = new SimpleDateFormat("hh:mm:ss",Locale.getDefault());

        System.out.println("time " + formatTarget.format(format.parse(dateStr)));

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Ravi
  • 34,851
  • 21
  • 122
  • 183
  • 1
    To potentially explain this a little for @DiieBarcia `parse()` seems to drop timezone information but `format()` will preserve it. http://stackoverflow.com/questions/18122608/simpledateformat-parse-loses-timezone – d0nut Aug 04 '15 at 12:49
  • What I can see in this answer is that "d" is never used. Also I tried and just get the ParseException – IIRed-DeathII Aug 04 '15 at 12:54
  • I have test this code and then only posted it here. check where you are getting parseException – Ravi Aug 04 '15 at 12:56
  • 1
    @DiieBarcia I actually ran this and it worked for me. – d0nut Aug 04 '15 at 12:56
  • mmmm is not working for me :S Getting "java.text.ParseException: Unparseable date: "Tue, 04 Aug 2015 12:09:10 GMT" (at offset 0)" – IIRed-DeathII Aug 04 '15 at 13:01
  • check carefully, 12:09: 10, there is space before 10, remove that space, or in simpleformat object write that space – Ravi Aug 04 '15 at 13:02
  • I dont see that space anywhere – IIRed-DeathII Aug 04 '15 at 13:10
  • still getting java.text.ParseException: Unparseable date: "Tue, 04 Aug 2015 12:09:10 GMT" (at offset 0) – IIRed-DeathII Aug 04 '15 at 14:21
1
public static void main(String[] args) throws ParseException {
    String dateStr = "Tue, 04 Aug 2015 12:09:10 GMT";
    SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
    Date d = format.parse(dateStr);
    System.out.println("The current time is: " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds());
    Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"), Locale.ENGLISH);
    cal.setTime(d);
    System.out.println("The current time is: " + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND));
}
eduyayo
  • 2,020
  • 2
  • 15
  • 35
0

The only way is removing the GMT from the string source. Because that indicates you wanna parse using the timezone. After that, you must remove the zzz as well like:

String dateStr = "Tue, 04 Aug 2015 12:09:10"

SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.ENGLISH);
Date d = format.parse(dateStr);

System.out.println("The current time is: " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds());
Lennon Spirlandelli
  • 3,131
  • 5
  • 26
  • 51
  • 1
    The first solution throws java.text.ParseException: Unparseable date: "Tue, 04 Aug 2015 14:16:25 GMT" (at offset 26). On the second solution I am getting the same problem with getHours – IIRed-DeathII Aug 04 '15 at 14:17
  • The only way is removing the `GMT` from the string source. Because that indicates that you wanna parse using the timezone. After that you must remove the `zzz` as well. – Lennon Spirlandelli Aug 04 '15 at 20:52
-1

for printing current time u can use this approach

 Calendar c = Calendar.getInstance(); 
int seconds = c.get(Calendar.SECOND);
int minutes=c.get(Calendar.MINUTE);
int hour=c.get(Calendar.HOUR);
int noon=c.get(Calendar.AM_PM);
int day=c.get(Calendar.DAY_OF_WEEK);
int date=c.get(Calendar.DATE);
int month=c.get(Calendar.MONTH)+1;
int year=c.get(Calendar.YEAR);

String currentTime="The current time is: "+String.valueOf(hour)+":"+String.valueOf(minutes)+":"String.valueOf(seconds);

Also u can use day year month etc from this Calender

farhan678
  • 85
  • 13
  • 1
    This should be a comment, not an answer – d0nut Aug 04 '15 at 12:32
  • Sorry brother but as i am new here... need 50 reputation for commenting in Questions – farhan678 Aug 04 '15 at 12:33
  • 1
    What is so difficult to understand about OP's question? They gave an example of what output they wanted and how they were attempting to achieve that currently. There is even relevant code snippets which is more than I can say for most questions – d0nut Aug 04 '15 at 12:35
  • 1
    @farhan678 Solution: Don´t! – eduyayo Aug 04 '15 at 12:37
  • I can't believe i'm saying this but by trying to answer the question, you made your post worse. – d0nut Aug 04 '15 at 12:45
  • its another way of achieving current date time... posted what i am also using – farhan678 Aug 04 '15 at 12:50
  • Have you tried reading the question? Op isn't trying to get the current time, per-say, he wants to get a specific time hence the String value. The value of said String being today may only be coincidental with the time of the post. – d0nut Aug 04 '15 at 12:51
  • I dont think so he clearly wrote in output string "THE CURRENT TIME IS" – farhan678 Aug 04 '15 at 12:53
  • From OP: *"I want to print the Time (hours:minutes:seconds) from a date gotten from a **String**."* Please read the question next time. – d0nut Aug 04 '15 at 12:55
  • C'mon @farhan678 be Smart. Do you thing I have to change the string name to "The current String time is..."?? – IIRed-DeathII Aug 04 '15 at 12:57
  • and u read the question to the end next time please System.out.println("The current time is: " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds()); – farhan678 Aug 04 '15 at 12:57
  • If I had enough reputation I would put -1 to this answer – IIRed-DeathII Aug 04 '15 at 13:03
  • 1
    StackOverflow isn't place for arguments – Lennon Spirlandelli Aug 04 '15 at 13:11
  • @Lennon but what about arguments in function calls? :p – d0nut Aug 04 '15 at 13:31