Im trying to display the current time in a format like 7:45pm instead of 19:45 but i can't seem to find the right format option.
Time cTime= new Time(Time.getCurrentTimezone());
cTime.setToNow();
clock.setText(cTime.format("%H:%M"));
This seems to display military
You're using Time.format(), so you can refer to the C++ strftime documentation for formatting rules. Your format string should be %I:%M%P for 12-hour time.
Use SimpleDateFormat with a - meaning am/pm marker.
Example:
Calendar cal = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("h:mmaa");
try {
String now = dateFormat.format(cal.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
Related
I'm receiving this Timestamp string from the server:
"timestamp": "2021-02-21T22:15:37.672+00:00" I'm not sure what format it's in
how I can format it to a Month day time format such as February 21st, 4:00pm?
Edit:
Using the answer below:
String time = "2021-02-21T22:15:37.672+00:00";
try {
Date dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").parse(time);
Log.d(TAG, "onCreate: " + dateFormat.toString());
} catch (ParseException e) {
e.printStackTrace();
}
"2021-02-21T22:15:37.672+00:00" is "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" in SimpleDateFormat in JAVA SE7.
"February 21st, 4:00pm" is "MMM dd, h:mma"
The "st" part seems like it have to be determine manually in SimpleDateFormat.
SimpleDateFormat
That most likely is UTC time. You can tell it is because it doesn't have an offset via the +00:00, which is used for timezone offset. UTC can be converted to other timeszones using DateFormat.
Ref:
https://developer.android.com/reference/java/text/DateFormat
I am accessing Dot net web services and using Ksoap library as my web services are Soap based.
Basically i want to save the dates and show them in list at it is. I do not want the date conversion to any specific region or time zone.
my dates which are coming from services has following pattern please have an example of threee different fields.
patient DOB = 1974-05-18T00:00:00
Collection Date = 2016-07-27T11:00:00
attachment Date uploaded = 2016-09-28T10:19:23.48
and I am using following method to convert them
public static Date convertStringToDate(String date,String dateFormat)throws Exception {
Date output = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
try {
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
output = simpleDateFormat.parse(date);
} catch (ParseException e) {
throw e;
}
return output;
}
where dateFormat is "yyyy-MM-dd" and whereas date could be any string shown above in above example.
Problems:
1> When I convert date using that method I sometimes get accurate time and date
2> Some times I observed slightly changed in time , like 2 to 7 hours shift in time. this is due to time zone conversion
3> Some times I observe a whol day shift. Let suppose if the date that was coming from server was 2016-09-28T10:19:23.48 after conversion it becomes 2016-09-27 to me .
Whats wrong ? How can I simple show date as it is from web services How can i get that except saving dates directly in strings and showing those strings by splitting.
Please help me.
Update
i am converting back my date object back to string to show on UI in following manner
public static String convertDateToString(Date date,String dateFormat)throws Exception {
String output = "";
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
output = simpleDateFormat.format(date);
} catch (Exception e) {
throw e;
}
return output;
}
To achieve that, you need to know the actual timezone and dateformat of the server. If you knew it already, the following snippet would be useful
public static String parseDateFormat(String incomingDate, String inputFormat, String outputFormat) {
String finalDate = null;
SimpleDateFormat input = new SimpleDateFormat(inputFormat); // server date format
input.setTimeZone(TimeZone.getTimeZone(SERVER_TIMEZONE)); // timezone set in the server
SimpleDateFormat output = new SimpleDateFormat(outputFormat); // format to which you want to convert
output.setTimeZone(Constants.UTC_TIMEZONE); // timezone to which you want to convert
try {
Date realDate = input.parse(incomingDate);
finalDate = output.format(realDate);
} catch (ParseException e) {
Log.e("Error", e.getMessage(),e);
}
return finalDate;
}
Setting the time zone on the date format adds an offset to the time stamp. And your server's time format doesn't include any time zone.
Remove the line simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
You generally need to set the time zone only when you intend to account for the difference between locales.
i would like to set the date format of my date picker like this:
yyyy-MM-dd HH:ss:mm
I hope this is the correct format for dates in databases. if not, please tell me which format is right.
also i would like to set the hour,minute and second of selected date like 0
Example:
2015-11-30 00:00:00
For this i use this code:
long dateTime = DatePicker.getCalendarView().getDate();
Date date = new Date(dateTime);
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
Android Studio tells me, that setHours,setMinutes and setSeconds is deprecated. what is the new way to set this values?
Use Calendar Object instead of Date:
Calendar c = Calendar.getInstance();
c.setTimeInMillis(DatePicker.getCalendarView().getDate(););
c.set(Calendar.HOUR,00);
like wise
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");
String dateString = "2015-11-30 00:00:00";
try {
Date date = df.parse(dateString);
df.applyPattern("yyyy-mm-dd hh:mm:ss");
String result = df.format(date);
}
catch (ParseException e){
e.printStackTrace();
}
User will input a date and he/she will input days to add to it.
Like this:
Date: 1/1/2015
Days to add: 20
The output should be 1/21/2015
I am wondering how to do that. I am beginner in android. T__T cries I tried other sources but i don't understand them at all. THANKS
THE USER WILL SUPPLY THE DATE TO ADD AND THE NUMBER OF DAYS TO ADD. All other sources only explain adding the current date with the number of days.
You need to parse the string to a date first.
SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yyyy");
try {
Date myDate = dateParser.parse("01/01/2015");
Calendar c = Calendar.getInstance();
c.setTime(myDate);
c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) + 20);
Date newDate = c.getTime();
String newFormattedDate = dateParser.format(newDate);//01/21/2015
} catch (ParseException e) {
e.printStackTrace();
//handle exception
}
I have a problem when I try to get only the time from a Timestamp.
An example of the Timestamp is:
2012-04-19T23:05:00+0200
I think the format is "yyyy-MM-dd'T'HH:mm:ss.SSSZ" right?
And it must be "HH:mm".
I use the following code, but it returnes nothing:
public String getTime(String Vertrektijd){
final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date dateObj;
String newDateStr = null;
try
{
dateObj = df.parse(Vertrektijd);
SimpleDateFormat fd = new SimpleDateFormat("HH:mm");
newDateStr = fd.format(dateObj);
}
catch (Exception e)
{
e.printStackTrace();
}
return newDateStr;
}
Thanks for the help!
Your code is correct...
In the example time what you have given in the question(ie, "2012-04-19T23:05:00+0200") is missing MilliSeconds
Try passing this
getTime("2012-04-19T23:05:00.235+0200");
It should work.
Edit:
As MH mentioned, If you dont want to use milliseconds
you can change the code to
final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date has a getHours() and getMinutes() function, but it is deprecated. The proper way would be to use a Calendar
Calendar calendar = Calendar.getInstance();
calendar.setTime( dateObj );
int hours = calendar.get( Calendar.HOUR_OF_DAY );
int minutes = calendar.get( Calendar.MINUTE );
Here is an attempt to summarize all the confusing classes that Java and Android provide to do with dates, times and timezones. Basically, you do most of your date/time manipulations using GregorianCalendar objects, probably using methods from the Calendar superclass. To do locale-specific formatting, you need a DateFormat. But that can only format Date objects, so you need to convert your Calendar/GregorianCalendar to one of those first. Basically, SimpleDateFormat is for doing custom formatting, as you’ve already discovered.
Note there are two different classes called “DateFormat”.