convert Time "HH:mm:ss.000Z" from Time "HH:mm:ss" - java

I am developing one Java application and getting data from webservice with parameter Time "HH:mm:ss.000Z" format.
How do I convert Time "HH:mm:ss.000Z" from Time "HH:mm:ss" format using below line but getting same error for time.
There is auto generated code from wsdl.
org.apache.axis.types.Time time=new org.apache.axis.types.Time("00:00:01");

Check implementation of class Time.
private static SimpleDateFormat zulu =
new SimpleDateFormat("HH:mm:ss.SSS'Z'");
This is SimpleDateFormat used by Time class to convert time to that format.

Related

Android time format issue on different devices [duplicate]

This question already has answers here:
Time parsing issue on Android
(3 answers)
Closed 5 years ago.
String time1= "12:45 PM"; Device1 default format ,
String time2= "12:45 p.m."; Device2 default format
These two times are same but looks different format in different devices. Testing with remi note4 (time1 format) and Samsung j7 (time2 format).
If I'm using device with default time format 'time1' then i cant convert string format 'time2' to timestamp. Logger shows unable parse date . How to resolve this problem?
NB: Time to be converted is fetching from server as JSON string format.
You can first convert it to same format then you will parse like :
String time1= "12:45 PM";
String time2= "12:45 p.m.";
String modify_time_2 = time2.replace("p.m.","PM").replace("pm","PM").replace("p m","PM");
You can use this modify_time_2 for converting it to time.
Another question of mine got the exact answer for the following issue. Please refer following link if anyone wants to know..Refer following link

Date format in JavaScript and Java

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

Using DateFormat.getDateTimeInstance().format(date);

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.

Custom date format in android for given locale

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.

Android Time Class format time

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.

Categories

Resources