![]() ![]() ![]() The Instant object is capable of carrying microseconds or nanoseconds, finer than milliseconds. long millisSinceEpoch = instant.toEpochMilli() īe aware of possible data loss. Now we can get your desired count of milliseconds since the epoch reference of first moment of 1970 in UTC, T00:00Z. 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). Now we have a specific moment, in that ZonedDateTime. So you must specify which you have in mind if you want to determine a specific point on the timeline. Is it 6:10 PM in India, Europe, or Canada? Each of those places experience 6:10 PM at different moments, at different points on the timeline. Without the context of a zone/offset, a LocalDateTime has no real meaning. LocalDateTime ldt = LocalDateTime.parse( input ) Ī LocalDateTime, like your input string, lacks any concept of time zone or offset-from-UTC. So no need to specify a formatting pattern. The java.time classes use these standard formats by default when parsing/generating strings. And replace the slash characters with hyphens. Replace the SPACE in the middle with a T. ![]() The modern approach uses the java.time classes.Īlter your input to conform with the ISO 8601 standard. Avoid the 3 or 4 letter codes which are neither standardized nor unique. Is your input string 6:10 PM in Paris or Montréal? Or UTC? The accepted answer is correct, except that it ignores the crucial issue of time zone. toEpochMilli() // Get the number of milliseconds since first moment of 1970 in UTC, T00:00Z. ZoneId.of( "Europe/Oslo" ) // Specify the time zone you are certain was intended for that input. atZone( // Apply a time zone to provide the context needed to determine an actual moment. replace( "/", "-" ) // Replace SLASH in the middle with a `-`. replace( " ", "T" ) // Replace SPACE in the middle with a `T`. "9 18:10:45" // Convert input string to comply with standard ISO 8601 format. Tl dr LocalDateTime.parse( // Parse into an object representing a date with a time-of-day but without time zone and without offset-from-UTC. Seems that you want/need to use the default zone of your system.Ĭheck which zone you need to use for specific behaviour e.g. Specify the Zone where the date/time will be used. With this new Date/Time API, when using a date, you need to LocalDateTime localDateTime = LocalDateTime.parse(myDate,ĭateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss") ) Using the new Date/Time API available since Java 8: String myDate = "9 18:10:45" Still, be careful because in Java the milliseconds obtained are the milliseconds between the desired epoch and 00:00:00. SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss") Here's an example of what you want/need to do (assuming time zone is not involved here): String myDate = "9 18:10:45" To convert a String into a Date and vice versa you should use SimpleDateFormat class. You should convert the String into a Date and then obtain the milliseconds. You don't have a Date, you have a String representation of a date. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |