+ 1

This code is not giving me the expected result please help me resolve it.

package Calendar; import java.text.SimpleDateFormat; import java.util.Calendar; public class date { public static void main(String[] args) { Calendar time = Calendar.getInstance(); SimpleDateFormat tTime = new SimpleDateFormat("hh:mm:ss"); tTime.format(time.getTime()); SimpleDateFormat tDate = new SimpleDateFormat("dd:mm:yy"); tTime.format(time.getTime()); System.out.print("The time is " + tTime +"\t"); System.out.print("Today's date is " + tDate); } }

8th Sep 2020, 7:53 PM
Sunday Othniel
Sunday Othniel - avatar
3 Respostas
+ 1
Anyway, both Calendar and Date classes are outdated. I suggest you to read about and use LocalDate, LocalTime and LocalDateTime.
8th Sep 2020, 8:28 PM
Aleksandrs
Aleksandrs - avatar
+ 1
Aleksandrs Kalinins write to me through inbox.
8th Sep 2020, 8:34 PM
Sunday Othniel
Sunday Othniel - avatar
0
SimpleDateFormat works with Date class. Try this: public class Example { public static void main(String[] args) { Date date = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss"); String strDate= formatter.format(date); System.out.println(strDate); } }
8th Sep 2020, 8:26 PM
Aleksandrs
Aleksandrs - avatar