How do I format time using Android's Time Class.
I want to display time in this format "hh:mm AM/PM" . I have tried using the Time.format function but I'm not sure if I'm using it correctly.
Thanks
Please try this..
SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm aa");
Date dt = new Date();
String strValue = timeFormat.format(dt);
Time time = new Time();
time.set(0, 0, 17, 4, 5, 1999);
Log.i("Time", time.format("%d.%m.%Y %H:%M:%S"));
try:
String strTime = time.format("%I:%M %p");
Your answer can be derived from, but not limited to this link, a C++ reference about "ctime":
http://www.cplusplus.com/reference/ctime/strftime/
I found this link to be very helpful deciphering the string formats used in my own work.
The Android.Text.Format.Time.Format docs assumes you know something:
Where or how to read "man" page for strftime, which if you weren't familiar using Linux (or a Mac at the terminal) might require some creative web searching or know what "See man strftime for what means what." referred to. Informal as it is in official documentation, it does build off and reference what has already has come before and is left as an exercise for the developer.
Related
I am running into an issue with Date parsing in IE 11. It seems to be occurring in Edge as well. Please have a look at the following Java code. This below code is part of a controller method that a form is submitted to.
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
stream.setAutoExpireDate(dateFormat.parse(request.getParameter("autoExpireDate")));
The output of request.getParameter("autoExpireDate") is "05/30/2018".
When parse is called on that specific date format, passing that string, it hits the following DateFormat.java function:
public Date parse(String source) throws ParseException
{
ParsePosition pos = new ParsePosition(0);
Date result = parse(source, pos);
if (pos.index == 0)
throw new ParseException("Unparseable date: \"" + source + "\"" ,
pos.errorIndex);
return result;
}
For some reason, and only in IE/Edge, I keep getting the following error:
Unparseable date: "05/30/2018"
I am very confused as to why this is an "unparseable" date. The same date string seems to work fine with Chrome and Firefox when I submit the form through the application.
Obviously, pos.index == 0 in the parse function is equaling 0 when the value is passed, but for what reason? Why would IE/Edge be behaving in such a manner?
I apologize if anything is unclear. Feel free to ask any questions and I will further clarify. Any help is greatly appreciated.
Best Regards,
MotoDave452
UPDATE: While request.getParameter("autoExpireDate") is a string that equals "05/30/2018", check out the differences in the actual value when using IE compared to Chrome.
IE:
Chrome:
It appears IE is adding non-printable characters to your date. Try the following:
dateStr = source.replaceAll("\\p{C}", "");
There is a problem in IE 11 Date Parser, doesn't like unicode characters, it mean, you have to replace them, with something like this
Date dateStr = new Date().toLocaleString().replace(/[^A-Za-z 0-9 \.,\?""!##\$%\^&\*\(\)-_=\+;:<>\/\\\|\}\{\[\]`~]*/g, '');
Date date = new Date(dateStr);
to avoid the non-asci characters
i'm learn Java thourgh Android Studio with Java, i was stuck at the Float Format, how can you display, for example: 1.234E10 instead of 12340000000000, thanks guys
12340000000000 is actually 1.234E13
DecimalFormat formatter = new DecimalFormat("0.###E0");
String format = formatter.format(12340000000000.0);
System.out.println(format);
Output:
1.234E13
1.234E10 is actually the same as 12340000000000 only it's printed out in an engineering format and it's a hell of a lot more readable than 12340000000000.
I have these time formats in Java which I use to format the date:
dd-mm-yyyy
dd-mm-rr
dd-mon-yyyy
dd-mon-rr
dd/mon/yyyy
When I use these time formats with JQuery time picker I get this output:
24-08-rr
24-08-20122012
Can you tell me how I can use one time format for Java and for JavaScript? How I can solve this problem?
For example you can use next:
-On client side
$("#input-field-id").datepicker().mask("99/99/9999");
-On server side
org.joda.time.format.DateTimeFormatter DATE_TIME_FORMATTER =
DateTimeFormat.forPattern("MM/dd/yyyy");
DATE_TIME_FORMATTER.parseDateTime(dateThatYouGatFromClientPart);
You can use latest version of jQuery UI datepicker which has the following useful functions
http://docs.jquery.com/UI/Datepicker/formatDate
http://docs.jquery.com/UI/Datepicker/parseDate
When running some tests I came across the following issue. When using:
private String printStandardDate(Date date) {
return DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT).format(date);
}
I found this produced different formats of Date depending on the location the tests where run from. So locally in windows / eclipse I got a result: 04/02/12 18:18 but on the Linux box in America I get 2/4/12 6:18 PM
This causes my Tests/Build to fail:
expected:<[04/02/12 18:18]> but was:<[2/4/12 6:18 PM]>
Could anyone explain this behavior?
That's not strange, that's exactly how it's supposed to work.
The API documentation of DateFormat.getDateTimeInstance says:
Gets the date/time formatter with the given date and time formatting styles for the default locale.
The default locale is different on your Windows system than on the Linux box in America.
If you want exact control over the date and time format, use SimpleDateFormat and specify the format yourself. For example:
private String printStandardDate(Date date) {
return new SimpleDateFormat("dd/MM/yy HH:mm").format(date);
}
Even better would be to re-use the SimpleDateFormat object, but beware that it is not thread-safe (if the method might be called from multiple threads at the same time, things will get messed up if those threads use the same SimpleDateFormat object).
private static final DateFormat DATE_FORMAT =
new SimpleDateFormat("dd/MM/yy HH:mm");
private String printStandardDate(Date date) {
return DATE_FORMAT.format(date);
}
The format is based on the default locale in your code. If you want to ensure results you must make sure to use a specific locale. The getDateTimeInstance method is overloaded to offer an alternative method that receives the locale that you want to use as parameter.
public static final DateFormat getDateTimeInstance(int dateStyle,
int timeStyle,
Locale aLocale)
If you use the same locale in both testing environments, the result should be the same.
I'm trying to format a date for a given locale new Locale("mk", "MK"). The locale is valid, it returns the country name and language properly. I want to use custom string, in my case "E, kk:mm" or "EEEE, kk:mm". I want the output to be "сабота, 12:00", but what I get is "7, 12:00".
This is how I use it and I tried many ways, but they all seem to behave the same.
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, kk:mm", new Locale("mk", "MK));
sdf.format(new Date());
// output: 7, 12:30
Another method I tried
Calendar calendar = Calendar.getInstance(new Locale("mk", "MK"));
calendar.setTimeInMillis(new Date().getTime());
DateFormat.format("EEEE, kk:mm", calendar);
// output: Saturday, 12:30
I also tried using java.text.DateFormat instead android class, but no change.
The phone locale is set to English, but this is localized app, I want to show dates in a fixed locale format.
I've looked into many SO question regarding this issue and I wasn't able to find answer. I'm not interested in predefined formats, I want to use my own format and I want the date/month names to be formatted for the input locale.
I think the problem is that Macedonia is not a supported locale on the Android JVM. If you run your code as plain Java console app, it's fine. The method Locale.getAvailableLocales() returns 152 members in plain Java, only 88 in an Android emulator. If you have the code snippet:
Locale[] locales = Locale.getAvailableLocales();
String cCode;
for (Locale loc :locales){
cCode = loc.getCountry();
if (cCode.equalsIgnoreCase("MK"))
Toast.makeText(this, cCode, Toast.LENGTH_SHORT).show();
// Or System.out.println() in a Java app
}
Then the toast doesn't show for "MK" although it will println in the Java app
From documentation of SimpleDateFormat:
**Text**: For formatting, if the number of pattern letters is 4 or more,
the full form is used; otherwise a short or abbreviated form is used if
available. For parsing, both forms are accepted, independent of the
number of pattern letters.
So this should fix it:
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, kk:mm", new Locale("mk", "MK"));
NickT was faster :-), so just adding to his answer: if you want to see your locales supported on Android, run:
for (Locale l:Locale.getAvailableLocales()) {
Log.d(l.getDisplayCountry(),l.toString());
}
and you will see that Macedonia is not on the list.