Skip to content Skip to sidebar Skip to footer

Getting Current Date And Time

I tried to access current datetime in android application as follows : Calendar c = Calendar.getInstance(); int seconds = c.get(Calendar.SECOND); //long time = Syste

Solution 1:

You have not set current date in calender object.

 Calendar c = Calendar.getInstance();
    c.setTime(newDate());
    //Calendar.SECOND will returnonly seconds from the date//int seconds = c.get(Calendar.SECOND);
    long time= c.getTime();
    Date date2 =newDate(time);
    Log.d(">>>>>>>Current Date : ",""+date2);

You can use SimpleDateFormat class to format the dates as follow

SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
System.out.println(format.format(new Date()));

Solution 2:

try this

privateStringgetCurrentDateAndTime() {
        SimpleDateFormat simple = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
        return simple.format(newDate());
    }

Solution 3:

Avoid old date-time classes

You are using the old date-time classes bundled with the earliest versions of Java such as java.util.Date/.Calendar. They have proven to be poorly designed, confusing, and troublesome. Avoid them.

Among the many points of confusion is that a java.util.Date represents a calendar date and a time-of-day while a java.sql.Date pretends to represent only a date without any time-of-day although it does actually have a time-of-day just set to zeros (00:00:00.0).

java.time

The old date-time classes have been supplanted by the java.time framework. See Tutorial.

java.sql

Eventually we should see JDBC drivers updated to deal with java.time types directly. Until then, we still need the java.sql types for getting data in/out of databases. But immediately call the new conversion methods added to the old classes to move into java.time types.

Instant

An Instant is a moment on the timeline in UTC with resolution up to nanoseconds.

java.sql.Timestampts= myResultSet.getTimestamp( x );
Instantinstant= ts.toInstant();

LocalDate

If you are trying to compare the date-portion of that date time to today’s date, use the LocalDate class. This class truly represents a date-only value without time-of-day and without time zone.

Time Zone

Note that a time zone is crucial to determining dates, as the date may vary around the world by time zone for any given moment. So before extracting the LocalDate we need to apply a time zone (ZoneId) to get a ZonedDateTime. If you omit the time zone your JVM’s current default time zone is implicitly applied. Better to specify explicitly the desired/expected time zone.

ZoneIdzoneId= ZoneId.of( "America/Montreal" ); // Or "Asia/Kolkata", "Europe/Paris", and so on.ZonedDateTimezdt= ZonedDateTime.ofInstant( instant , zoneId );
LocalDatetoday= LocalDate.now( zoneId );
if( today.isEqual( zdt.toLocalDate() ) {
    …
}

Notice that nowhere in that code did we use Strings; all date-time objects instead.

Formatting Strings

To generate a String as a textual representation of the date-time value, you can call the toString methods to get text formatted using the ISO 8601 standard. Or specify your own formatting pattern. Better yet, let java.time do the work of localizing automatically. Specify a Locale for a human language (English, French, etc.) to use in translating the name of day/month and such.

DateTimeFormatterformatter= DateTimeFormatter.ofLocalizedDateTime( FormatStyle.MEDIUM );
Stringoutput= zdt.format( formatter.withLocale( Locale.US ) );  // Or Locale.CANADA_FRENCH and so on.

Post a Comment for "Getting Current Date And Time"