Hello Devs,
I'm working on barcode scanner app, I get date and time at this pattern "20220610T230000Z"
I think its ISO8601 date-time format
However, I just want to parse this pattern so I can customize it as I want.
I tried this one:
val isoDate="20220610T230000Z" // from my barcode scanner
val df=SimpleDateFormat("yyyymmdd'T'HH:mm:ss.SSS'Z'")
val date= df.parse("20220610T230000Z")
but when i run code i get
java.text.parseexception unparseable
Thanks in advance
isoDate doesn't have any colons or periods in it. Since you're trying to turn isoDate into a Date object, you want something more like:
val df=SimpleDateFormat("yyyymmdd'T'HHmmssSSS'Z'")
Then, if you want to output a date String with different formatting, you'll have to create a different SimpleDateFormat instance (let's call it dt2) with the intended formatting, and call dt2.format(date). See here for a full example.
My issue solved
Thanks #OleV.V.
Solution
This pattern: "yyyyMMdd'T'HHmmss"
Related
I have a string as a date formatted yyyy-MM-dd, and I want to compare it with the current date. I'm using Android api24, and I want to be able to tell how much time has passed in a format like the first string.
I have tried with the Calendar class, something like this:
val firstDate=Calendar.getInstance()
val dateFormat=SimpleDateFormat("yyyy-MM-dd",Locale.getDefault())
firstDate.time=dateFormat.parse("2001-06-04")
but I get stuck here, getting the current time as a calendar object.
You can desugar to get to use the Java 8 datetime features:
Java 8+ APIs available through desugaring
Then it is really simple:
private fun convertFromString(datetime: String): LocalDateTime {
val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.getDefault())
return LocalDateTime.parse(datetime, dateTimeFormatter)
}
Is there any other way to get custom date in android except that java date picker
It means that you're trying to parse a date that doesn't match the pattern you choose. You should have a look at the simple date documentation to see which pattern you need: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
This link might help too: http://developer.android.com/reference/java/text/SimpleDateFormat.html
I encounter strange behaviour with Jodatime and Android. I want to parse string:
2014-05-19T18:13:00.000+02:00
to DateTime, and get year, month, hours to int. I started with some test on IntelliJ Studio, and I done something like that:
String date = "2014-05-19T18:13:00.000+02:00";
DateTime dateTime = new DateTime(date);
System.out.println(dateTime.toString());
System.out.println(dateTime.getYear());
System.out.println(dateTime.getMonthOfYear());
System.out.println(dateTime.getDayOfMonth());
System.out.println(dateTime.getHourOfDay());
System.out.println(dateTime.getMinuteOfHour());
System.out.println(dateTime.getMillis());
Which gave me correct answers:
2014-05-19T18:13:00.000+02:00
2014
5
19
18
13
1400515980000
Now, when I changed IDE to Android Studio, and do the same:
String dateT = "2014-05-19T18:13:00.000+02:00";
DateTime dateTime = new DateTime(dateT);
Lo.g(dateTime.getHourOfDay() + "");
Lo.g(dateTime.toString());
my results are:
16
2014-05-19T16:13:00.000Z
For some reason DateTime on Android Studio / Android not take into account the timezone which is +2:00.
I can not find solution for this. Also there is no simple method "addTimeZone" in Joda.
How to display correct time with DataTime? I tried LocalDateTime, construct DateTime with DateTimeZone.getDefault() (which gaves me UTF...)
Since you said that you use the same Joda-Time version on both platforms and regarding the fact that Joda-Time has its own timezone repository independent from system timezone data, there is probably only one explanation left why you observe different behaviour: Different input either explicit or implicit. Let's go into details:
Well, you say, obviously there is the same input given the same input string:
"2014-05-19T18:13:00.000+02:00"
So we have the same (explicit) input. But wait, there is another thing: implicit default settings which can also be considered as kind of input in an abstract way. You use the constructor DateTime(Object). This constructor first delegates to super constructor of class BaseDateTime as you can see in the source code.
public DateTime(Object instant) {
super(instant, (Chronology) null);
}
The javadoc of this super-constructor says:
"Constructs an instance from an Object that represents a datetime,
using the specified chronology. If the chronology is null, ISO in the
default time zone is used.
The recognised object types are defined in ConverterManager and
include ReadableInstant, String, Calendar and Date."
So finally we see that Joda-Time uses the default timezone. This is really the only possibility for different behaviour I can see by studying the source code and the documentation. All other things are equal: Same library version and same explicit string input and same test scenario.
Conclusion: You have different default timezones on your platforms. Note that you get the same instant on both platforms however, just represented with different local timestamps and offsets due to different internal timezone/offset setting inside the DateTime-object.
Update: I have tested the zone overriding behaviour with this code:
String date = "2014-05-19T19:13:00.000+03:00"; // same instant as yours (just with offset +03)
DateTime dateTime = new DateTime(date);
System.out.println(dateTime.toString());
// output: 2014-05-19T18:13:00.000+02:00 (in my default timezone Europe/Berlin)
So the default timezone has precedence over any offset in string input. If you instead want to use the parsed offset then look at using the DateTimeFormatter and its method withOffsetParsed().
I m developing android application. I need to convert datetime into date. I want to convert '25-07-2013 11:44AM' (datetime) into '25-07-2013' (date).
I am trying this function to convert SELECT date('25-07-2013 11:44AM'), but it was not working.Please suggest some solution for this problem.
According to this page, it does not seem that am/pm times are supported in date SQLite function (this should be noted as h or K according to the date format specification of Java at least; also it is explicitly mentioned %H hour: 00-24). Maybe experiment if using 24 hour clock will not trigger the issue.
I am not sure if you are search for Java code or SQL code , but if it is Java code then solution could be this :
String date = "25-07-2014 11:44AM";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String newDate = dateFormat.format(dateFormat.parse(date));
System.out.println(newDate);
If in case your are looking for SQL code let me know I will share that as well.
I need to communicate with one application that is not implementing the full ISO 8601 and only accept the format +HH:mm for time zone offset.
Android seems to only generate the format +HHmm (no ':' character in between hours and minutes) with the 'z' in SampleDateFormat.
Code example:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String str = String.format(Locale.US, "%s", sdf.format(new Date(0)));
generates: 1970-01-01T00:00+0000
and I would like to generate: 1970-01-01T00:00+00:00
Is there any simple way of producing the desirable output without writing the code to manipulate the string?
Thanks,
Luis
I think the easiest way is to manipulate a string.
a) SimpleDateFormat doesn't support what you need.
b) The method appendNumericTimeZone in SimpleDateFormat class is private. So, you can't override it.
2) You can create your own formater (implement java.text.DataFormat). However, it will be way more hassle than string manipulation.
BTW. Interesting thing which I found while looking into SimpleDateFormat source code. There is some code which generates almost what you need (it adds to the end "GMT+XX:XX"). However, this code will be only called, if you specified "Z" in the format and system can't find a timezone name for current timezone.