Android Studio Convert ISO string to America/New_York when adding to event to calendar
That is because you're telling the parser to ignore the Z
, or rather to match it literally, but not to process it's meaning.
The input strings are Instant
values, so parse to Instant
, then apply time zone.
If you don't have Java 8+ compatible Android, use ThreeTenABP.
String stTime = "2018-10-17T22:00:00Z";
ZonedDateTime time = Instant.parse(stTime).atZone(ZoneId.of("America/New_York"));
System.out.println(time); // prints: 2018-10-17T18:00-04:00[America/New_York]
If you prefer using antiquated SimpleDateFormat
, simply use the correct format, and don't specify time zone.
SimpleDateFormat formatStart = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
Date startDate = formatStart.parse(stTime);
// My default time zone is America/New_York, so:
System.out.println(startDate); // prints: Wed Oct 17 18:00:00 EDT 2018
startDate.getTime()
is the time-in-millis you need.
For API Level < 24, format pattern X
doesn't work, so you need to do the hardcoded Z
like in your question, but tell the parser that it is in UTC, because that is what the Z
time zone means.
SimpleDateFormat formatStart = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
formatStart.setTimeZone(TimeZone.getTimeZone("UTC"));
Date startDate = formatStart.parse(stTime);
// My default time zone is America/New_York, so:
System.out.println(startDate); // prints: Wed Oct 17 18:00:00 EDT 2018
How to transfer this kind of DateFormat 2020-05-29T11:40:55Z to Date
I didn't quite understand the last part of your question. Are you saying that you cannot use java 8 in your project ? .For java 8 based , use ZonedDateTime
like :
//1 - default pattern
String timeStamp = "2019-03-27T10:15:30";
ZonedDateTime localTimeObj = ZonedDateTime.parse(time);
//2 - specified pattern
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss a z");
String timeStamp1 = "2019-03-27 10:15:30 AM";
ZonedDateTime localTimeObj1 = ZonedDateTime.parse(timeStamp1, formatter);
//To get LocalDate from ZonedDateTime
LocalDate localDate = localTimeObj1.toLocalDate();
Using the old way to get the date :
String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss a";
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
TimeZone tzInAmerica = TimeZone.getTimeZone("America/New_York");
formatter.setTimeZone(tzInAmerica);
String sDateInAmerica = formatter.format("2019-03-27 10:15:30 AM");
//Create another simpledateformatter with your required format and use it to convert
SimpleDateFormat formatter2 = new SimpleDateFormat(REQUIRED_DATE_FORMAT);
Date date = formatter2.parse(sDateInAmerica);
Android change date format from String to Date and Time separated
A more robust way of accomplishing this task is provided below with a code example.
String initialStringDate = "2019-01-27T09:27:37Z";
Locale us = new Locale("US");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", us);
try {
Date date = format.parse(initialStringDate);
String stringDate = new SimpleDateFormat("yyyy/MM/dd", us).format(date);
String stringTime = new SimpleDateFormat("HH:mm", us).format(date);
String finalDateTime = stringDate.concat(" ").concat(stringTime);
Log.i("Date_and_Time", "" + finalDateTime);
} catch (ParseException e) {
e.printStackTrace();
}
Note: With the provided above code you will be able to even localize your date and time.
Get time in specific format from timestamp including TimeZone
The outputtime is in that format because you specified it as so ( HH:mm
) in this line
val outputFormat: DateFormat = SimpleDateFormat("HH:mm", Locale.getDefault())
if you want it to include the time zone append the time zone to the output string
edit
"HH:mm zZ"
seems to be doing the trick for me check out this fiddle
Java : Date String to Hours
tl;dr
Instant
.parse( "2020-07-26T20:08:27Z" )
.isBefore(
Instant.parse( "2020-07-26T21:08:27Z" )
)
true
java.time
Use the modern java.time classes built into Java 8 and later.
ISO 8601
The Z
on the end of your input indicates an offset-from-UTC of zero hours-minutes-seconds, or UTC itself. The Z
is pronounced “Zulu”.
Your input strings are in standard ISO 8601 format. The java.time classes use these formats by default when parsing/generating strings. So no need to specify a formatting pattern.
Instant
An Instant
object represents a moment in UTC.
Instant instantA = Instant.parse( "2020-07-26T20:08:27Z" ) ;
Instant instantB = Instant.parse( "2020-07-26T21:08:27Z" ) ;
Compare using equals
, isBefore
, isAfter
.
boolean aBeforeB = instantA.isBefore( instantB ) ;
Duration
You can capture the time elapsed between the two moments as a Duration
object.
Duration d = Duration.between( instantA , instantB ) ;
You can ask the Duration
object if it is zero or negative.
How to format current string into a date/time format?
The modern approach uses the java.time classes.
Instant instant = Instant.parse( "2019-02-22T13:43:00Z" ) ;
OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MMM-uuuu HH:mm" ) ;
String output = odt.format( f ) ;
output :
22-Feb-2019 13:43
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
.
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.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
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.
Parse UTC time format into readable datetime in android
You can use SimpleDateFormat
to format the date. Check below:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(sdf.parse("2019-12-06T06:04:50.022461Z"));
} catch (Exception ex) {
ex.printStackTrace();
}
SimpleDateFormat returnFormat = new SimpleDateFormat("dd-mm-yyyy hh:mm:ss");
returnFormat.format(calendar.getTime());
Related Topics
How to Convert Image to Byte Array in Java
Custom JSON Deserialization with Jackson
How to Use Jndi Datasource Provided by Tomcat in Spring
Why Do I Need Transaction in Hibernate for Read-Only Operations
Different War Files, Shared Resources
How to Fix Google Cloud Messaging Registration Error: Service_Not_Available
Opengl Extensions Available on Different Android Devices
What Is Difference Between @+Id/Android:List and @+Id/List
Onactivityresult() Not Called in New Nested Fragment API
Issue When Using a Custom Font - "Native Typeface Cannot Be Made"
Android HTML.Fromhtml() with Images
How to Serialize Object to JSON
Jar Mismatch Found 2 Versions of Android-Support-V4.Jar in the Dependency List
Wait Until Firebase Retrieves Data
Unsupported Major.Minor Version 52.0 in My App
How to Implement "CSS Versioning" (To Solve Cache Issues) Using Jsf 2 H:Outputstylesheet