1

I am extracting the date field from the sms table which returns the date as a time stamp, but I wish to extract from the time stamp seprately

  • time of the day,
  • day of the week and
  • month of the year

I have read similar questions but none could help.

    String date = cursor.getString(cursor.getColumnIndex("date"));
    Long timestamp = Long.parseLong(date);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(timestamp);
    Date finaldate = calendar.getTime();
    String smsDate = finaldate.toString();
    dateTextView.setText(smsDate);
gmetax
  • 3,853
  • 2
  • 31
  • 45
Michael Okoli
  • 4,887
  • 2
  • 16
  • 20
  • FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. Much of the java.time functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Feb 05 '18 at 22:08

4 Answers4

3
int day= calendar.get(Calendar.DAY_OF_WEEK));
int month = calendar.get(Calendar.MONTH);

If you want to have the actual string value of month and the day, you can either use a switch to do that

Try the following,

String dayAsString = calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());

String monthAsString = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());

Edit:

    int hour12 = calendar.get(Calendar.HOUR); // 12
    int hour24 = calendar.get(Calendar.HOUR_OF_DAY); //24
diyoda_
  • 5,274
  • 8
  • 57
  • 89
0

You can do calendar.MONTH, calendar.DAY and so.

Fustigador
  • 6,339
  • 12
  • 59
  • 115
0

you should use Joda Time:

http://www.joda.org/joda-time/

String date = cursor.getString(cursor.getColumnIndex("date"));
Long timestamp = Long.parseLong(date);
DateTime dateTime = new DateTime(timestamp);
int dayOfWeek = dateTime.getDayOfWeek();
int monthOfYear = dateTime1.getMonthOfYear();
Norutan
  • 1,480
  • 2
  • 14
  • 27
  • FYI, the [Joda-Time](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [java.time](http://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Feb 05 '18 at 21:54
0

tl;dr

Instant.ofEpochMilli( myCountOfMillis )                       // Parse a count of milliseconds since first moment of 1970 in UTC.
    .atZone( ZoneId.of( "Pacific/Auckland" ) )                // Instantiate a `ZonedDateTime` object, having adjusted from UTC into a particular time zone.
    .getDayOfWeek()                                           // Extract a `DayOfWeek` enum object.
    .getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH )  // Or Locale.US, Locale.ITALY, etc. to determine human language and cultural norms in generating a localized string to represent this day-of-week.

lundi

java.time

Modern approach uses the java.time classes.

Assuming your long integer is a count of milliseconds since the epoch reference of first moment of 1970 in UTC, 1970-01-01T00:00Z, instantiate a Instant. The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instant instant = Instant.ofEpochMilli( myCountOfMillis ) ;

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" ) ;  

Apply your desired/expected time zone to the Instant to get a ZonedDateTime. Same moment, same point on the timeline, but with a different wall-clock time.

ZonedDateTime zdt = instant.atZone( z ) ;

Now you are in a position to interrogate for the parts of month, day-of-week, and time-of-day.

Month month = zdt.getMonth() ;                   // Get a `Month` enum object.
int m = zdt.getMonthValue() ;                    // Get an integer 1-12 for January-December.

DayOfWeek dow = zdt.getDayOfWeek() ;             // Get a `DayOfWeek` enum object.
int dowNumber = zdt.getDayOfWeek().getValue() ;  // Get a number for the day-of-week, 1-7 for Monday-Sunday per ISO 8601 standard.
String dowName = zdt.getDayOfWeek().getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) ;  // Generate a localized string representing the name of this day-of-week.

LocalTime lt = zdt.toLocalTime() ;               // Get a time-of-day object, without date, without time zone.

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154