Android Calendar DateFormat regional behavior - android

In my android application I get the time and the date with the following code:
Calendar calendar = Calendar.getInstance();
String date = DateFormat.getDateInstance().format(calendar.getTime());
String time = DateFormat.getTimeInstance().format(calendar.getTime());
In my Galaxy Note 4, I got:
Date: 29.07.2016
Time: 14:10:30
In Huawei Mate 8:
Date: 29.07.2016
Time: 02:10:30 nachm (what means pm).
On some customer devices it has some more formats.
So, my question is how can I get date and time on all devices and regions in the same format?
I want dd.mm.yyyy for date and hh:mm:ss for time.

One options is SimpleDateFormat
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dataFormater = new SimpleDateFormat();
dataFormater.applyPattern("dd.MM.yyyy");
String date = dataFormater.format(calendar.getTime());
dataFormater.applyPattern("hh:mm:ss");
String time = dataFormater.format(calendar.getTime());

I think date will be ok for you so You have to manually format the time from calendar like below.
public static final String TIME_FORMAT = "hh:mm:ss";
SimpleDateFormat TimeFormat = new SimpleDateFormat(TIME_FORMAT);
Calendar ATime = Calendar.getInstance();
String Timein12hourFormat = TimeFormat.format(ATime.getTime());
May this help you.Let me know.

Related

Could not print time after conversion from string format

// For Date validation
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd/MM/yyyy");
String datechosen = dateText.getText().toString() ;
Date dateselected = simpleDateFormat1.parse(datechosen);
System.out.println(dateselected);
// For Time validation
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
String timechosen = timeText.getText().toString();
Date timeselected = simpleDateFormat.parse(timechosen);
Calendar cal = Calendar.getInstance();
cal.setTime(timeselected);
cal.add(Calendar.HOUR, noofhourselected);
cal.add(Calendar.MINUTE, noofminselected);
timeselected = cal.getTime();
System.out.println(timeselected);
I am working on converting the string which i have into the date and time format. For example, the string datechosen contain "26/10/2020". I am able to to convert it into date format and print it out.
But for the time string, i am unable to print them out. I am facing the error below:
Screenshot of the log message
But if i swap the position of the codes the other way round,
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
String timechosen = timeText.getText().toString();
Date timeselected = simpleDateFormat.parse(timechosen);
Calendar cal = Calendar.getInstance();
cal.setTime(timeselected);
cal.add(Calendar.HOUR, noofhourselected);
cal.add(Calendar.MINUTE, noofminselected);
timeselected = cal.getTime();
System.out.println(timeselected);
// For Date validation
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd/MM/yyyy");
String datechosen = dateText.getText().toString() ;
Date dateselected = simpleDateFormat1.parse(datechosen);
System.out.println(dateselected);
The time will be printed instead
These are the two input fields
You are setting date in wrong format for time. for cal.setTime(timeselected); setTime takes Date Refer java doc
Use same format as used for Date.

Split and get date and time separately from Google Calendar Api DateTime

I am learning android and working on Google Calendar Api
By using this code below in my app
DateTime start = event.getStart().getDateTime();
i am getting this result
2015-12-02T14:15:00.000+05:00
Is there any way to split and get date and time separately in below way
Date : 2015-12-02
Time : 14:15:00.000
GMT : 05:00
Use SimpleDateFormat
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddEHH:mm:ss.SSSX");
try {
calendar.setTime(sdf.parse("2015-12-02T14:15:00.000+05:00"));
} catch (ParseException e) { }
You can use the Calendar object to get any data you want
The code below should do the trick you can change the format in SimpleDateFormat as you wish also the time you get will in 24-hour clock format (hour:minutes:seconds:milliseconds)
Calendar cal = Calendar.getInstance();
Date today = cal.getTime();
SimpleDateFormat sdfD = new SimpleDateFormat("dd/MM/YYYY");
date = sdfD.format(today);
SimpleDateFormat sdft = new SimpleDateFormat("HH:mm:ss:SSS");
time = sdft.format(today);

Android - How to add days to date

I have retrieved a Date from a SQLiteDatabase and have formatted it to how I want via the following;
String steepingDate = (c.getString(3));
SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy");
Date steepingdate = formatter.parse(steepingDate);
I now want to give the user the option to increase whatever date is in steepingdate by a certain amount of days that the user can input
I know you can use;
Date today = calendar.getTime();
calendar.add(Calendar.DAY_OF_YEAR, 10);
For example to add 10 days onto todays date
But how do you do it so that it uses steepingdate instead of todays date
Thanks
UPDATE;
The calendar is working as I want, but I now want to save the new data to the database, the full code is as following;
String steepingDate = (c.getString(3));
SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy");
Date steepingdate = formatter.parse(steepingDate);
Integer amountDays = Integer.parseInt(TSExtend.getText().toString());
Calendar ca = Calendar.getInstance();
ca.setTime(steepingdate);
ca.add(Calendar.DAY_OF_YEAR, amountDays);
DateFormat dateFormat = new SimpleDateFormat("dd/MMM/yyyy");
String newDate = dateFormat.format(ca);
I'm getting the error;
Bad class: class
java.util.GregorianCalendar
Any ideas?
To add 10 days to steepingdate, you can use:
Calendar calendar = Calendar.getInstance();
calendar.setTime(steepingdate);
calendar.add(Calendar.DAY_OF_YEAR, 10);
it the number is provided, through the user interface, you can use the View.OnClickListener and when onClick is fired, read the value from an EditText, and use this value instead of 10
Set the time of the calendar to your date, then add the days
Calendar cal = Calendar.getInstance();
cal.setTime(steepingdate);
cal.add(Calendar.DATE, 10);
UPDATE:
You can't directly format a Calendar, first get the Date from the Calendar, then format it.
String newDate = dateFormat.format(ca.getTime());

Joda-Time and Android system time

Currently Joda DateTime can get the current Android system time, but is it possible to get the DateTime when say the Android system date or timezone has changed?
I have tried this after the Date/Timezone has changed but it does not work:
DateTime dt = DateTime.now();
DateTime dt = new DateTime();
When I use the Calendar it works fine:
Calendar c = Calendar.getInstance();
c.getTime() //Returns the correct changed Android Date and Time
As suggested by this post, you can get the Calendar time first:
public static int elapsedDaysJoda() {
Calendar cal = Calendar.getInstance();
DateTime dt1 = new DateTime(cal);
cal.set(2011, 0, 1);
DateTime dt2 = new DateTime(cal.getTime());
Days days = Days.daysBetween(dt2, dt1);
return days.getDays();
}

Incorrect Result: Formatting ISO-8601 Time And Extract 12 Hour Time

I am fetching Newsfeeds from the Facebook API using FQL which returns a "created_time" field as a UNIX Timestamp. I am converting that into, what I believe is a ISO-8601 timestamp using this piece of code:
String getInitialCreatedTime = JOFeeds.getString("created_time");
long finalTimeStamp = Long.valueOf(getInitialCreatedTime);
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
String finalCreatedTime = dateFormatter.format(new Date(finalTimeStamp * 1000L));
Now, from the String, finalCreatedTime I want to extract just the time in 12 Hour (AM/PM) format.
To that effect, I am using this code:
final String old_format = "yyyy-MM-dd'T'HH:mm:ssZ";
final String new_format = "EEE MMM d hh:mm aa yyyy";
String oldDateSring = finalCreatedTime;
SimpleDateFormat sdf = new SimpleDateFormat(old_format);
Date d = sdf.parse(oldDateSring);
sdf.applyPattern(new_format);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
And:
// GET THE TIME
SimpleDateFormat sdfTimeFormatter = new SimpleDateFormat("hh:mm aa");
String getTime = sdfTimeFormatter.format(cal.getTime());
feeds.setTime(getTime);
Log.e("TIME", getTime);
The result of the first code block is: 2012-12-14T04:30:03+0530
And the result from the // GET THE TIME block is 04:30AM when it should be 04:30PM.
I would appreciate any pointers on this. Perhaps, I am implementing it wrong?
EDIT: I might add, that timestamps that fall between 12.00 PM and 1.00 PM are handled properly and show PM as they should.
You have :
String getInitialCreatedTime = JOFeeds.getString("created_time");
long finalTimeStamp = Long.valueOf(getInitialCreatedTime);
// Note 8601 is written with 'HH'
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
So far, so good. But then you need to create a date from this.
Date date = new Date(finalTimeStamp * 1000L)
Then, you need to format what you need (and never EVER parse a date you just formatted. That makes no sense at all).
String finalCreatedTime = dateFormatter.format(date); // Not sure if you need this one
And
SimpleDateFormat sdfTimeFormatter = new SimpleDateFormat("hh:mm aa");
String getTime = sdfTimeFormatter.format(date);
feeds.setTime(getTime);

Categories

Resources