1

I would like to generate a DateTime variable from two different variables (get the date from myLongDateAndTime and time from myStringTime, how can I do that?

String myStringTime="12:30:10"; // Come from DB
long myLongDateAndTime= 1628197200000 // Come from another DB stores date and times in timestamp format of Thu Aug 05 2021 17:00:00 GMT-0400

DateTime myDateTime=??? // should get Thu Aug 05 2021 12:30:10
Mehran Ishanian
  • 369
  • 2
  • 4
  • 13
  • Does this answer your question? [Java string to date conversion](https://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – jhamon Aug 03 '21 at 15:14
  • 1
    @jhamon - It is not as simple as the requirement in your link :). Earlier, I was also not very sure about which question I should mark as duplicate but after going through [Handling Duplicate Questions](https://stackoverflow.blog/2009/04/29/handling-duplicate-questions/), an article by one of the founders of SO, I understood it better. In a nutshell, a question should be marked as duplicate when the requirement is exactly the same. – Arvind Kumar Avinash Aug 03 '21 at 16:02
  • 1
    @jhamon thanks for your answer but my problem was I read that DateTime is mutual and can not be changed and here I would like to combine 2 different conversions that was a bit confused. – Mehran Ishanian Aug 03 '21 at 16:36
  • 1
    Dear @ArvindKumarAvinash , thanks for your complete answer, appreciate it. then there is neither setDate nor setTime to do something like ``` new DateTime().setTime(myStringTime).setDate(myLongDateAndTime.toMillis()) ``` ? – Mehran Ishanian Aug 03 '21 at 16:39
  • @MehranIshanian - You are most welcome. One of the aims of Joda Date-Time API and later, `java.time` API was to make the Date-Time objects immutable. Check [this great discussion](https://stackoverflow.com/questions/1969442/whats-wrong-with-java-date-time-api) to learn more about it. – Arvind Kumar Avinash Aug 03 '21 at 16:41

3 Answers3

3

java.time

Quoted below is a notice from the home page of Joda-Time:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Solution using java.time, the modern Date-Time API:

import java.time.Instant;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class Main {
    public static void main(String[] args) {
        String myStringTime = "12:30:10";
        long myLongDateAndTime = 1628197200000L;

        LocalTime time = LocalTime.parse(myStringTime);
        System.out.println(time);

        Instant instant = Instant.ofEpochMilli(myLongDateAndTime);
        System.out.println(instant);

        OffsetDateTime odt = instant.atOffset(ZoneOffset.of("-04:00"));
        System.out.println(odt);

        odt = odt.with(time);
        System.out.println(odt);
    }
}

Output:

12:30:10
2021-08-05T21:00:00Z
2021-08-05T17:00-04:00
2021-08-05T12:30:10-04:00

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.

Just for the sake of completeness

Just for the sake of completeness, given below is the solution using the Joda Date-Time API:

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalTime;

public class Main {
    public static void main(String[] args) {
        String myStringTime = "12:30:10";
        long myLongDateAndTime = 1628197200000L;

        LocalTime time = LocalTime.parse(myStringTime);
        System.out.println(time);

        DateTime dateTime = new DateTime(Long.valueOf(myLongDateAndTime), DateTimeZone.forOffsetHours(-4));
        System.out.println(dateTime);

        dateTime = dateTime.withTime(time);
        System.out.println(dateTime);
    }
}

Output:

12:30:10.000
2021-08-05T17:00:00.000-04:00
2021-08-05T12:30:10.000-04:00

* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

You are combining two dates, so what you need to do is:

  • create a joda DateTime from the long
  • format that DateTime to a string with only the date part
  • combine with date string and time string in a single string
  • parse the new string

and here is how you can do that:

public DateTime combineDates(long myLongDateAndTime, String myStringTime) {
    // 1 - create DateTime from the long
    DateTime dateFromLong = new DateTime(myLongDateAndTime);

    // 2 - Format dateFromLong as date string
    DateTimeFormatter dtfDate = DateTimeFormat.forPattern("dd/MM/yyyy");
    String dateString = dtfDate.print(dateFromLong);

    // 3 - Concatenate date part and time part in a new string
    String completeDate = dateString + " " + myStringTime;

    // 4 - Parse the new string in to a DateTime
    DateTimeFormatter dtfDateTime = 
    DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
    return dtfDateTime.parseDateTime(completeDate);
}

This is only a possible solution. There are many other ways to do the same, for example without using a string concatenation, but only dates operations, but this way is quite clear and readable, so I don't investigate additional possible solutions.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
1

One possible solution:

public class TestSample {

    public static void main(String[] args) {
        String myStringTime="12:30:10";
        Long myLongDateAndTime= 1628197200000L;

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss", Locale.ENGLISH);
        LocalTime time = LocalTime.parse(myStringTime, formatter);
        
        LocalDate date = Instant.ofEpochMilli(myLongDateAndTime).atZone(ZoneId.of("UTC")).toLocalDate();
        
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
        LocalDateTime dtTime = LocalDateTime.parse(date.toString()+" "+time.toString(), formatter1);
        System.out.println(dtTime.toString());
    }
}
Nick
  • 1,017
  • 1
  • 10
  • 16