I added this jar file joda-time-2.0.jar in my android project . After adding this jar file it cannot taken the .ofpattern() , please solve this one. thank you.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Using Joda time you would do it like this:
String dateTime = "11/15/2013 08:00:00";
// Format for input
DateTimeFormatter dtf = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
// Parsing the date
DateTime jodatime = dtf.parseDateTime(dateTime);
// Format for output
DateTimeFormatter dtfOut = DateTimeFormat.forPattern("MM/dd/yyyy");
// Printing the date
System.out.println(dtfOut.print(jodatime));
Related
I have the following String:
18/07/2019 16:20
I try to convert this string into LocalDateTime with the following code:
val stringDate = expiration_button.text.toString()
val date = LocalDateTime.parse(stringDate, DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm")).toString()
java.time.format.DateTimeParseException: Text '18/07/2019 04:30:00'
could not be parsed: Unable to obtain LocalDateTime from
TemporalAccessor
What I'm missing?
I think this will answer your question:
val stringDate = expiration_button.text.toString()
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm");
val dt = LocalDate.parse(stringDate, formatter);
Edit 1:
It's probably crashing because you are using a 12hr Hour, instead of a 24hr pattern.
Changing the hour to 24hr pattern by using a capital H should fix it:
val dateTime = LocalDateTime.parse(stringDate, DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"));
Use below to convert the time from String to LocalDateTime, but make sure you are getting the time in String form.
String str = "2016-03-04 11:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
Btw, If your String contains seconds as well like "2016-03-04 11:30: 40", then you can change your date time format to yyyy-MM-dd HH:mm:ss" as shown below:
String str = "2016-03-04 11:30: 40";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
Change your datetime format to "dd/MM/yyyy hh:mm a" and provide a string date with additional AM/PM information, e.g. val stringDate = "18/07/2019 04:20 PM" or just use the 24 hour format "dd/MM/yyyy HH:mm".
You may try using "-" instead of "/" on the date.
I have this from my server:
2017-04-07T11:00:00.000Z
How can I convert it to local time? Here is my code, but all I got is an exception:)
java.lang.IllegalArgumentException: Invalid format: "2017-04-07T11:00:00.000Z" is malformed at "Z"
DateTimeFormatter inputFormatter = DateTimeFormat
.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ") //yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ
.withLocale(Locale.US);
DateTime parsed = inputFormatter.parseDateTime(gmtDate);
DateTimeFormatter outputFormatter = DateTimeFormat
.forPattern("HH:mm")
.withLocale(Locale.US)
.withZone(DateTimeZone.getDefault());
return outputFormatter.print(parsed);
Just change your pattern to yyyy-MM-dd'T'HH:mm:ss.SSSZ, like this:
DateTimeFormatter inputFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
.withLocale(Locale.US);
In my case, the result of outputFormatter.print(parsed) is 08:00 (my local time, as my default timezone is America/Sao_Paulo)
I have in String variable this ... "2015-01-12 19:00:00" So I want to convert to DateTime value , I have tried with this....
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy HH:mm:ss");
Date d = dateFormat.parse(fecha);
But I can't compile, I have gotten this message ... cannot convert from java.util.Date to java.sql.Date
How fix it?
The pattern is actually wrong, try this
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = dateFormat.parse(fecha);
Check if you have imported java.sql.Date instead of java.util.Date.
I am trying to parse the following str to Date:
2013-10-23T11:00:00EDT
I am using follwoing to do so:
String TIMEZONE_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssz";
Date date = new SimpleDateFormat(TIMEZONE_DATE_TIME_FORMAT, Locale.US).parse(strDate);
its working good on most devices but on some it throws the following exception:
java.text.ParseException: Unparseable date: "2013-10-23T11:00:00EDT" (at offset 19)
Can someone explain why?
Try this code. This may help you to fix the exception
SimpleDateTime format = new SimpleDateTime("yyyy-MM-dd'T'HH:mm:ssX", Locale.US);
Date date = format.parse(text);
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ")
.withLocale(Locale.US);
DateTime dateTime = formatter.parseDateTime(text);
In fact, there's an ISODateTimeFormat class to make this even simpler:
DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis();
I am working on simple application to show the time in different formate. To show the time I am displaying ISO country code using this ISO code. Can I able to change time in that ISO country format?
I have written code as follows
TelephonyManager tMgr =
(TelephonyManager)getApplicationContext().getSystemService(
Context.TELEPHONY_SERVICE);
String iso = tMgr.getNetworkCountryIso();
Log.v("Device iso", "=======>" + iso);
String mbNo = tMgr.getLine1Number();
Log.v("mbNo", "=======>" + mbNo);
Here I am getting iso as US. Can I show the current system time format in US time format?
I have used Date class to show time as follows
DateFormat df = DateFormat.getTimeInstance();
systime = df.format(new Date());
It is displaying time in HH:MM:SS AM/PM format. I would like to display the above time as US format.
How can I display the time in US or any other format?
In your case I think you could use
Date systemDate = Calendar.getInstance().getTime(); // the current system time
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT, Locale.US);
Date myDate = df.format(systemDate);
Or to use custom format
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date myDate = sdf.format(systemDate);
if you have a string use parse
Date myDate = sdf.parse("29/04/1980 12:30:24");
DateFormat has a static method that can initalize a localized format.
I believe that you need to create your own method for US format. Try to create a class and make its super class a DataFormat class and simply add your method.