0

I have some dates in firebase and i need to retrieve as Miliseconds and do some operations with it. the Date are like String in format "dd/MM/yyyy"

I tryed with a code like this, :

String myDate = "2014/10/29 18:10:45";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = sdf.parse(myDate);
long millis = date.getTime();

My code, basically i tryed to get the storeged String from firebase and convert to a Date for compare with the current day and show the diference of days. the error that I have, is only in the word "parse" of my code

refresh.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                           String mydate = model.getParto();
                           SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                           Date date = sdf.parse(mydate);

                        }
                    });

but parse set an error

image of code and the string date in firebase

enter image description here

String date in firebase

hope, someone can help

Armayeestr
  • 17
  • 5
  • 3
    Please include the error. I would also recommend [this](https://stackoverflow.com/questions/882420/parse-string-to-date-with-different-format-in-java). – Adrian Russo Jan 15 '21 at 20:52
  • 1
    Removing the Firebase tag, as it's not relevant to the code shown here. – Doug Stevenson Jan 15 '21 at 21:02
  • 2
    Show code as text, not images. – Basil Bourque Jan 15 '21 at 21:13
  • 1
    `parse` throws a `ParseException`, which you must catch. But as answered, dont use `SimpleDateFormat` – OneCricketeer Jan 15 '21 at 21:24
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jan 15 '21 at 21:38

1 Answers1

1

tl;dr

LocalDateTime
.parse( 
    "2014/10/29 18:10:45".replace( " " , "T" ) 
)
.atOffset( ZoneOffset.UTC ) 
.toInstant()
.toEpochMilli() 

Need epoch reference

Representing a moment as a count of milliseconds requires a point in time as an epoch reference. You need to state the reference needed in your situation. I will assume the commonly used point of first moment of 1970 in UTC. But there are a couple dozen other points used by various systems. So you need to find out the meaning of your own data.

Need time zone or offset

Determining a moment requires more than a date and a time-of-day. You also need the context of a time zone or offset-from-UTC. Again, you need to specify this but did not. Is your example of ten minutes past six in the evening in Tokyo Japan, Toulouse France, or Toledo Ohio US? I will assume you mean an offset of zero hours-minutes-seconds. But again, you need to find out the meaning of your own data.

Avoid legacy date-time classes

Never use SimpleDateFormat, Date, or the other terrible date-time classes that were supplanted years ago by the modern java.time classes defined in JSR 310.

Date versus moment

Your Question in confused, referring to a date-only value as well as a date with time-of-day represented as milliseconds. These are two different kinds of data.

If representing a date-only, use LocalDate in Java and a type in your database akin to the SQL-standard DATE. I will ignore this date-only, and focus on tracking a moment.

Example code

Parse your input as LocalDateTime, after complying with standard ISO 8601 format by replacing SPACE in middle with a T.

A LocalDateTime does not represent a moment, is not a point on the timeline. You need to discover the zone/offset intended for you input, and apply. Apply the time zone intended for your input, to produce a ZonedDateTime. Or, if UTC (an offset of zero) was intended, apply a ZoneOffset to get an OffsetDateTime object. At this point we have determined a moment.

Extract a Instant object from the OffsetDateTime. Interrogate for a count of milliseconds since the epoch reference of 1970-01-01T00:00Z.

String myDate = "2014/10/29 18:10:45".replace( " " , "T" ) ;  // Comply with ISO 8601 standard formatting.
LocalDateTime ldt = LocalDateTime.parse( input ) ;
OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;  // Assuming your data was intended to represent a moment as seen in UTC, with an offset of zero hours-minutes-seconds.
Instant instant = odt.toInstant() ;  // Basic building-block class in java.time, representing a moment as seen in UTC.
long millisecondsSinceEpoch1970 = instant.toEpochMilli() ;

Your title mentions Firebase, but that seems irrelevant to your Question. so I will ignore that topic.

All the content in this Answer has been covered many times already on Stack Overflow. Search to learn more.


About java.time

Table of all date-time types in Java, both modern and legacy

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.

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

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

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

Basil Bourque
  • 218,480
  • 72
  • 657
  • 915