0

Issue:

I need to save a timestamp in SQLITE Database (Android) and extract date from it. Which is the best way LocalDateTime or Instant.now()? as this Android application is being used in Saudi Arabia.

Requirement:

I must query the Timestamp column for the very first Timestamp entered in DB and extract date alone from it to compare it with the present day date to check if the day difference exceeds 30 days.

Need help with:

  1. The best of two LocalDateTime or Instant.now()
  2. How to extract the date alone from that best way. Appreciate help.

So Far done:

I have used LocalDateTime to save Timestamp but not sure how to extract date only from it.

Gladiator
  • 169
  • 1
  • 2
  • 12
  • Best way is `Instant.now()` as it records 1-Date, 2-Time and 3-Moment [ the moment at one place has a time difference in other parts of the world ] Using `ZonedDateTime zdt = now.atZone(ZoneId.of("Asia/Riyadh"));` I was able to capture a timestampmoment of Saudi Arabia. Others please pass appropriate `ZoneId` for other countries. – Gladiator Aug 27 '20 at 08:56
  • To extract Date alone from `ZonedDateTime` stamp, use `LocalDate today = zdt.toLocalDate();` – Gladiator Aug 27 '20 at 08:58
  • For Business centric apps, `ZonedDateTime` is more preferable than `LocalDateTime` – Gladiator Aug 27 '20 at 09:05

2 Answers2

1

You can use this (available for Api 26+):

String ts = String.valueOf(Instant.now().getEpochSecond());

Support for older Android versions:

String ts = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));

Or if you want it in a specific date format use this:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String millisInString  = dateFormat.format(new Date());
Itiel Maimon
  • 834
  • 2
  • 9
  • 26
  • Hi Itiel! Thank you, But like i said, I need the answer using Java.time as `SimpleDateFormat` is not recommended in 2020. – Gladiator Aug 27 '20 at 07:54
  • Use my first suggestion then, it uses Java.time. – Itiel Maimon Aug 27 '20 at 07:58
  • Okay, let me try and get back on this. – Gladiator Aug 27 '20 at 08:02
  • Also do you think `Instant.now()` is preferrable than `LocalDateTime ` If so, how? Tried your first suggestion and it results a string of seconds. whereas Instant.now() alone gives [ 2020-08-27T08:22:14.841Z ] – Gladiator Aug 27 '20 at 08:18
  • I suggest you to take a look here, it’s a very elaborate explanation of the differences. https://stackoverflow.com/a/32443004/6299883 – Itiel Maimon Aug 27 '20 at 08:21
  • The answer by Basil Bourque has information about the same I required. Thank you, @Itiel Maimon I will post an appropriate answer after a trial – Gladiator Aug 27 '20 at 08:37
  • Good to hear. If you found my answer helpful please marked it as such. – Itiel Maimon Aug 27 '20 at 08:42
  • I would like to but Arvind has just posted an exact answer to extract date from a `ZonedDateTime` stamp `// Get the LocalDate part of this ZonedDateTime LocalDate today = zdt.toLocalDate(); System.out.println(today); ` As I only have few reputations my vote is not being registered. – Gladiator Aug 27 '20 at 08:46
0

Given below is all that you asked for:

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;

public class Main {
    public static void main(String[] args) {
        // The current moment at UTC time-scale
        Instant now = Instant.now();
        System.out.println(now);

        // The number of milliseconds from the epoch of 1970-01-01T00:00:00Z.
        long millis = now.toEpochMilli();
        System.out.println(millis);

        // The number of seconds from the Java epoch of 1970-01-01T00:00:00Z.
        long seconds = now.getEpochSecond();
        System.out.println(seconds);

        // Zone ID of Saudi Arabia
        ZoneId zoneId = ZoneId.of("Asia/Riyadh");

        // Get ZonedDateTime from Instant
        ZonedDateTime zdt = now.atZone(zoneId);
        System.out.println(zdt);

        // Get the LocalDateTime part of this ZonedDateTime
        LocalDateTime ldt = zdt.toLocalDateTime();
        System.out.println(ldt);

        // Get the LocalDate part of this ZonedDateTime
        LocalDate today = zdt.toLocalDate();
        System.out.println(today);

        // An instant in the past
        Instant past = LocalDateTime.of(LocalDate.of(2020, Month.APRIL, 10), LocalTime.MIDNIGHT)
                .toInstant(ZoneOffset.ofHours(3));// Zone Offset of Saudi Arabia is UTC +3
        System.out.println(past);

        // No. of months
        long months = ChronoUnit.MONTHS.between(past.atZone(zoneId), zdt);
        System.out.println(months);
    }
}

Output:

2020-08-27T08:45:31.927452Z
1598517931927
1598517931
2020-08-27T11:45:31.927452+03:00[Asia/Riyadh]
2020-08-27T11:45:31.927452
2020-08-27
2020-04-09T21:00:00Z
4
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110