How to get the current date - date () and calendar ()

I want the hours and minutes to start from the current date on October 10, 2016.

package com.mkyong.date; import java.text.SimpleDateFormat; import java.util.Date; public class DateDifferentExample { public static void main(String[] args) { DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); //get current date time with Date() Date date = new Date(); System.out.println(dateFormat.format(date)); //get current date time with Calendar() Calendar cal = Calendar.getInstance(); System.out.println(dateFormat.format(cal.getTime())); String dateStart = "01/14/2012 09:29:58"; String dateStop = "01/15/2012 10:31:48"; //HH converts hour in 24 hours format (0-23), day calculation SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); Date d1 = null; Date d2 = null; try { d1 = format.parse(dateStart); d2 = format.parse(dateStop); //in milliseconds long diff = d2.getTime() - d1.getTime(); long diffSeconds = diff / 1000 % 60; long diffMinutes = diff / (60 * 1000) % 60; long diffHours = diff / (60 * 60 * 1000) % 24; long diffDays = diff / (24 * 60 * 60 * 1000); System.out.print(diffDays + " days, "); System.out.print(diffHours + " hours, "); System.out.print(diffMinutes + " minutes, "); System.out.print(diffSeconds + " seconds."); } catch(Exception e) { e.printStackTrace(); } } } 

Results:

 2016/08/15 18:54:03 2016/08/15 18:54:03 1097 Days1 Hours 1 Minute 50 Second My want to result for example : 100 days 5 hours 2 minutes 
+5
source share
2 answers

Avoid Old Time Classes

You use the nasty old obsolete time classes associated with the earliest versions of Java. Use java.time classes instead.

Syntactic

Your input lines are almost in accordance with ISO 8601. Replace SPACE in the middle with T The java.time classes parse / generate strings according to ISO 8601 formats by default. Therefore, there is no need to specify a formatting pattern.

 String startInput = "01/14/2012 09:29:58".replace( " " , "T" ); String stopInput = "01/15/2012 10:31:48".replace( " " , "T" ); 

LocalDateTime

Your entries are missing any information about offset-from-UTC or time zone. So, we analyze how LocalDateTime objects.

 LocalDateTime startLdt = LocalDateTime.parse( startInput ); LocalDateTime stopLdt = LocalDateTime.parse( stopInput ); 

If you continue to work with these types, you will get results based on common 24-hour days, ignoring anomalies such as daylight saving time (DST).

If you know that the context of this data is a specific time zone, use the zone to get the ZonedDateTime .

 ZoneId zoneId = ZoneId.of( "America/Montreal" ); ZonedDateTime start = startLdt.atZone( zoneId ); ZonedDateTime stop = stopLdt.atZone( zoneId ); 

If you want the current moment to start or stop, call now . Pass on your desired / expected time zone, rather than relying on a valid time zone JVMs.

 ZonedDateTime now = ZonedDateTime.now( zoneId ); 

Duration

Duration class represents the time span as the total number of seconds plus the fraction of a second in the resolution of nanoseconds.

 Duration duration = Duration.between( start , stop ); 

Oddly enough, in Java 8 this class has no methods for getting the number of days, hours, etc., that make up this period of time. Java 9 adds methods to…Part .

 long days = duration.toDaysPart(); int hours = duration.toHoursPart(); int minutes = duration.toMinutesPart(); 

Prior to Java 9, you can do the math yourself.

About java.time

The java.time framework is built into Java 8 and later. These classes supersede old inconvenient time classes such as java.util.Date , .Calendar and java.text.SimpleDateFormat .

The Joda-Time project, now in maintenance mode , advises switching to java.time.

To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations.

Most of the functionality of java.time will be ported back to Java 6 and 7 in ThreeTen-Backport and additionally implemented with Android capability in ThreeTenABP .

The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter , etc.

+3
source

public final void set (int year, int month, int date) - this Calendar class method can be used to set the date.

public final void set (int year, int month, int date, int hourOfDay, int minute, int second) can also be used to set the time.

Calendar.getInstance () sets the current date and time by default.

+1
source

All Articles