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.
Related
I am trying to convert "2021-05-14T13:42:48.000Z" string to Date Object.
I have tried this:
DateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DDHH:MM:SS");
And also this, which i saw on stackoverflow only:-
DateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD'T'HH:MM:SS'A'");
But none of it worked.
How can i convert this string to my date object?
Assuming your date string always represents a UTC time (with the 'Z'), you can use format string:
yyyy-MM-dd'T'HH:mm:ss.SSSZ
but you'll first need to replace the Z in your date string with the fixed timezone "+0000", as in "2021-05-14T13:42:48.000+0000".
Try this:
String myDateString = "2021-05-14T13:42:48.000Z"
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
Date myDate = dateFormat.parse (myDateString.replace("Z","+0000"));
This will return a date correctly adjusted for your current timezone, in my case 9:42:48am EDT.
There is a more detailed discussion at Converting ISO 8601-compliant String to java.util.Date which you may find useful.
You have used the date-time format incorrectly. It's important to note that the date-time formats have different meanings between capitalized and small letters.
For example: Capital MM means months, whereas small mm means minutes.
To know more about the date formats, you can refer this:
https://cheatography.com/pezmat/cheat-sheets/date-time-formats/pdf/
or this:
https://devhints.io/datetime
And the answer for your case is:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Please do not use SimpleDateFormat or even java.date. All these classes are deprecated.
Instead, rely on the Android available java.time package.
In short:
val source = "2021-05-14T13:42:48.000Z"
val parsed = ZonedDateTime.parse(source)
This will correctly parse the timezone (Z for Zulu/UTC/GMT).
You can verify this, by simply converting the parsed Zoned date time into, for example, Europe/Amsterdam time (which is +2).
val source = "2021-05-14T13:42:48.000Z"
val parsed = ZonedDateTime.parse(source)
parsed.toString() // prints: 2021-05-14T13:42:48Z
parsed.zone // prints: "Z"
ZoneId.of(parsed.zone.id) // returns the ZoneOffset "Z" (correct)
// Convert to Amsterdam Time
val amsterdamDateTime = parsed.withZoneSameInstant(ZoneId.of("Europe/Amsterdam"))
amsterdamDateTime.toString() // prints: 2021-05-14T15:42:48+02:00[Europe/Amsterdam] (2 hours ahead of the Zulu time, also correct).
parsed.format(DateTimeFormatter.ISO_DATE_TIME).toString() // Prints: 2021-05-14T13:42:48Z (correct)
So as you can see, these classes do the right thing (most of the time).
I suggest you use them.
I create a URL using a given date as a parameter.
This date is created thusly:
final String from = DateFormat.format("dd.MM.yyyy", date).toString();
This string is added to a URL:
+ "&from=" + from
Errors started appearing in my Crashlytics reporter. To debug, I set it to report the URLs that were being sent.
How is it that the URL being generated can look like this: &from=٢٩.٠٦.٢٠١٦?
As Blackbelt has noted, the date is formatted as such because of the Locale on the user's phone.
You could simply convert the date to the format you want specifying English locale.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.ENGLISH);
String from = dateFormat.format(date);
That's due to your phone's default Locale.
Use this to get the date in correct format :
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd.MM.yyyy", Locale.ENGLISH);
String from = simpleDateFormat.format(date);
Read more about SimpleDateFormat here.
I create a date and then format is like this:
Example 1:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
String currentDate = sdf.format(new Date());
What I would like to do is check if this date is before another date (also formatted the same way). How would I go about doing this?
Example 2:
Also, how would I check whether one of these is before another:
long setForLong = System.currentTimeMillis() + (totalTime*1000);
String display = (String) DateFormat.format("HH:mm:ss dd/MM/yyyy", setForLong);
EDIT:
I think more detail is needed. I create a date in two different ways for two different uses. The first use just formats the current date into a string so it is readable for the user. In the second case, I am using a date in the future with System.currentTimeMillis and adding on a long. Both result in a string.
Both methods format the date in exactly the same way, and I set the strings into a TextView. Later, I need to compare these dates. I do not have the original data/date/etc, only these strings. Becasue they are formatted in the same way, I though it would be easy to compare them.
I have tried the if(String1.compareTo(String2) >0 ) method, but that does not work if the day is changed.
If you only have two String objects that are dates available to you. You will need to process them in something, either in your own comparator class or in another object. In this case, since these are already formatted into dates, you can just create Date objects and compare using the methods previously posted. Something like this:
String string = "05:30:33 15/02/1985";
Date date1 = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy", Locale.ENGLISH).parse(string);
String string2 = "15:30:33 01/02/1985";
Date date2 = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy", Locale.ENGLISH).parse(string2);
if(date1.getTime()>date2.getTime()) {
//date1 greater than date2
}
else if(date1.getTime()<date2.getTime()) {
//date1 less than date2
}
else {
//date1 equal to date2
}
You should use Calendar for convenient comparing dates.
Calendar c1 = Calendar.getInstance();
c1.setTime(Date someDate);
Calendar c2 = Calendar.getInstance();
c2.setTime(Date anotherDate);
if(c1.before(c2)){
// do something
}
And you can format it at any time
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
String currentDate = sdf.format(c1.getTime());
I have a date/time that is specified in milliseconds which is retrieved by:
Calendar.getInstance().getTimeInMillis();
I would like to format this to a string that represents the date and time in whatever the local format is on the device. For example:
US: 12/15/2013 10:30 pm
Germany: 15.12.2013 22:30
I shouldn't have to specify the formatting such as:
SimpleDateFormat outputFormat = new SimpleDateFormat("MMM dd, yyyy h:mm a");
Is there an API that handles local formatting?
See this code
String myformat = android.provider.Settings.System.getString(getContentResolver(), android.provider.Settings.System.DATE_FORMAT);
DateFormat dateFormat;
if (TextUtils.isEmpty(myformat)) {
dateFormat = android.text.format.DateFormat.getMediumDateFormat(getApplicationContext());
} else {
dateFormat = new SimpleDateFormat(format);
}
so dateformat will match with your device date
check DateFormat. Specially
public static final DateFormat getDateInstance(int style,
Locale aLocale)
Here is an example. See if it can help.
I guess this is what you are looking for:
Locale locale = Locale.getDefault();
Date today = new Date();
SimpleDateFormat.SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.LONG, SimpleDateFormat.LONG, locale).format(today));
where you need to check locale and pass its value accordingly.
As an option you can store a format string in string resources to make it available via
getResources().getString(R.string.format)
Then just put strings you need to locale folders, for example:
res/values-en/strings.xml
res/values-fr/strings.xml
I want to store the app launch time in sqlite and use that time for the next launch in the functionality .. for that i am using
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
String data = format.format(new Date(19900101));
System.err.println("Time is.."+format.format(19900101));
I stuck how to get the time dynamically in format of yyyy-MM-dd kk:mm:ss.Even though using this code i am getting 1970-01-01..Please suggest me right way to do this
Change your code to
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
String data = format.format(new java.util.Date());
System.err.println("Time is.."+data);
You able to take time via this
String timeStamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
OR change your code like this.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
String data = format.format(new Date());
System.err.println("Time is.."+format.format());
Thanks.