How to print only time in java? [duplicate]

212
July 27, 2019, at 3:20 PM

This question already has an answer here:

  • Change date format in a Java string 18 answers
DateFormat dateFormatOne = new SimpleDateFormat("HH:mm:ss");
dateFormatOne.setTimeZone(TimeZone.getTimeZone("UTC"));
Date dateOne = dateFormatOne.parse("10:00:00");
Answer 1
  • format convert date to string
  • parse convert string to date
public static void main(String[] args) throws ParseException {
    DateFormat dateFormatOne = new SimpleDateFormat("HH:mm:ss");
    dateFormatOne.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date dateOne = dateFormatOne.parse("10:00:00");
    System.out.println(dateFormatOne.format(dateOne));
    System.out.println(dateFormatOne.format(new Date()));
}

Output

10:00:00
20:20:55

But I recommend Java 8

System.out.print(LocalTime.of(10, 0, 0));
Answer 2

If you want to display the current local time on the machine running the program you can use LocalTime.

import java.time.LocalTime; // import the LocalTime class
public class Main { 
  public static void main(String[] args) { 
    LocalTime myTime = LocalTime.now();
    System.out.println(myTime);
  } 
}
READ ALSO
Outlook wont preview ics file created by ical4j

Outlook wont preview ics file created by ical4j

When sending ics attachments created by ical4j via sendgrid to an outlook user they aren't able to "preview" the inviteIt recognizes that the attachment exists but claim "This file cannot be previewed because there is no previewer installed for it"

151
Return Array from Java to Python

Return Array from Java to Python

Greetings i'm trying to return Array to Python from java(jar file) but only return array type string:

212
Alert message validation - Test Automation (Selenium)

Alert message validation - Test Automation (Selenium)

Good Morning! I'm catching a lot in a settingCould you help me?

110
xText- doGenerate returns xtend symbolics when run as a warfile

xText- doGenerate returns xtend symbolics when run as a warfile

I am developing an xText project and in my Generatorxtend I produce a python script

138