Joda-Time and Android system time - android

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();
}

Related

How to fetch recent seven days from specific date

If the date is 2017-03-30 that i want to fetch the date from 2017-03-23 to 2017-03-30
I try to use this code let my String change to Date format
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dateParse = sdf.parse("2017-03-30");
then i'm stuck , cause i take the reference is get the current time like this
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -7);
//may be my dateParse should put here , but i don't know how to do
Date monday = c.getTime();//it get the current time
String preMonday = sdf.format(monday);
Is any one can teach me how to fetch these seven days ? Thanks in advance.
You can use the code below
SimpleDateFormatdateFormat = new SimpleDateFormat("dd MMM yyyy");
Calendar c = Calendar.getInstance();
String date = dateFormat.format(c.getTime());
c.add(Calendar.DATE, 7);
String date1 = dateFormat.format(c.getTime());
Parse the date:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = sdf.parse("2017-03-30");
First Solution 1) And then either figure out how many milliseconds you need to subtract:
Date newDate = new Date(myDate.getTime() - 604800000L); // 7 * 24 * 60 * 60 * 1000
Second Solution 2) Or use the API provided by the java.util.Calendar class:
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -7);
Date newDate = calendar.getTime();
Then, if you need to, convert it back to a String:
String date = dateFormat.format(newDate);
This answer is from here
EDIT:
If you need output as 2017-03-29 2017-03-28 2017-03-27 ...... 2017-03-23 then try below code
for(int i = 1; i <= 7; i++){
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -i);
Date newDate = calendar.getTime();
String date = dateFormat.format(newDate);
//here in date you can get all date from and output as 2017-03-29 2017-03-28 2017-03-27 ...... 2017-03-23
}
Hope you need this

Android Calendar DateFormat regional behavior

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.

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());

Calendar not returning GMT

I am trying to get a calendar object set to GMT, but the getTime() always returns the time in GMT+1 (my current time). I have tried:
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("skeniver"));
They all apparently return GMT, because
cal.getTimeZone().getDisplayName()
returns "GMT+00:00"; but
cal.getTime().toString();
always displays the time in GMT+1.
Does anyone have any idea why this is happening?
You need to adjust for daylight savings. I'm not sure if this will help but it's code I use for adjusting any timezone to UTC in an app that's currently being used by a number of people around the world. I use Date instead of Calendar but it works...
Date dateTimeNow = new Date();
TimeZone tz = TimeZone.getDefault();
int currentOffsetFromUTC = tz.getRawOffset() + (tz.inDaylightTime(dateTimeNow) ? tz.getDSTSavings() : 0);
Date dateTimeNowUTC = new Date(dateTimeNow.getTime() - currentOffsetFromUTC);
If you want to in string then prefer the DateFormat or SimpleDateFormat for this
here is example
SimpleDateFormat sdf = new SimpleDateFormat(); // here you can also define your format of date for e.g. "dd/MM/yyyy z"
sdf.setTimeZone("GMT");
Calendar cal = Calendar.getInstance();
System.out.println(sdf.format(cal.getTime()));
Calendar.getTime() returns a Date object. In Java, a Date is just a holder to a long timestamp starting in the UNIX epoch.
To display a Date in a different TimeZone than the default, you can use a SimpleDateFormat.

Categories

Resources