Change Days to Dates in Timetable for Android - java

Here is code to show Mon-Fri . I do not want to show the Days (Mon-Fri). I want to show the Dates (DD-MMM-YYY). How can I change my code. Please help me take a look.Thanks.
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);
pager.setAdapter(buildAdapter(res));
if (day == 1)
{
int finalDay = 6;
pager.setCurrentItem(finalDay);
}
else
{
int finalDay = day - 2;
pager.setCurrentItem(finalDay);
}
pager.setOffscreenPageLimit(7);
Here is the ScreenShot:

You can use SimpleDateFormat class to show the dates like this:
String pattern="dd-MMM-yyy";
SimpleDateFormat sdf = new SimpleDateFormat();
sdf.applyPattern(pattern);
String format = sdf.format(Calendar.getInstance().getTime());

Related

java Calendar, sum month, year not changing

How can the year in the output add up according to the month's summation?
int sewa = 10
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
String cyear = Integer.toString(year);
int mont = (now.get(Calendar.MONTH) + 1);
String cmont = Integer.toString(mont);
int day = (now.get(Calendar.DATE) );
String cday = Integer.toString(day);
String date = cyear +"/"+cmont+"/"+cday;
Calendar ex = Calendar.getInstance();
int exyear = ex.get(Calendar.YEAR);
String excyear = Integer.toString(exyear);
ex.add(Calendar.MONTH,+sewa);
int exmont = (ex.get(Calendar.MONTH) + 1);
String excmont = Integer.toString(exmont);
int exday = (ex.get(Calendar.DATE) );
String excday = Integer.toString(exday);
String exdate = excyear +"/"+excmont+"/"+excday;
System.out.println(date);
System.out.println("+++++");
System.out.println(exdate);
now : 2018/3/25
+++++ after:2018/1/25
how ouput :2019/1/25
If you are using Java 8 or newer version, please use LocalDate instead of Calendar class. It's very easy to add days, months and years using methods plusDays(), plusMonths() and plusYears(). It's that simple:
LocalDate today = LocalDate.now();
today.plusDays(20);
today.plusMonths(1);
today.plusYears(5);
LocalDate and LocalDateTime were introduced with JSR-310 as much simpler, straightforward and easy to use replacement of Date and Calendar.
However, if you use older version of java, or need to use Date and Calendar for some other reason, you can increase the number of years the same way you did with the months, using
ex.add(Calendar.YEAR, 5);
You capture the year to a variable ...
int exyear = ex.get(Calendar.YEAR);
String excyear = Integer.toString(exyear);
... right before you add 10 months to the calendar ...
ex.add(Calendar.MONTH,+sewa);
... which causes the excyear remains not updated.
To solve it, give the statements a correct order (Get Calendar -> add the months -> get Strings -> print). Or better using SimpleDateFormat:
Calendar ex = Calendar.getInstance();
ex.add(Calendar.MONTH, sewa);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
String exdate = sdf.format(ex.getTime());
System.out.println(exdate);
Prints out:
2019/01/25

why use different date format makes different result in same date?

I try to use current date in date formats but when I use different date formats this makes different results..at first I used this code:
private String getTodayDateString() {
Calendar cal = Calendar.getInstance();
int month=cal.get(Calendar.MONTH);
return Integer.toString(month);
}
and this return me 5 for result for month.
but when I use this code:
private String getTodayDateString2() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
return dateFormat.format(cal.getTime());
}
function returns me 14/6/2016 and this means month is calculated 6 in this dateformat.why?where is the problem?
months in calender starts from 0
0 =january
11=december
if you see the source code of Calendar class you will find public final static int JANUARY = 0;
similarily for december public final static int DECEMBER = 11;
Check the source code here

Take away a single day using Calendar in Java?

I have an application that plugs into the Google Fit Api and returns the steps for the last 7 days, the method is below. As the screen shot shows though I wish to add the day to the step count.
I have tried many options to take away one day at a time for the 7 loop but had no luck, it just says the same day. Any help would be great thank you.
private void dumpDataSet(DataSet dataSet) {
Log.i(TAG, "Data returned for Data type: " + dataSet.getDataType().getName());
DateFormat dateFormat = DateFormat.getTimeInstance();
int i = 0;
for (DataPoint dp : dataSet.getDataPoints()) {
for(Field field : dp.getDataType().getFields()) { //loop 7 times
int test = dp.getValue(field).asInt();
String weekSteps= String.valueOf(test); //get weekday steps one at a time
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
Calendar cal = Calendar.getInstance();
String weekday = sdf.format(cal.getTime());
String weekStepsFinal= weekSteps + " steps on " + weekday; //set Textfield to steps and the day
FeedItem item = new FeedItem();
item.setTitle(weekStepsFinal);
feedItemList.add(item);
}
}
}
There are 7 datasets btw.
If by "take away one day at a time" means that you want the days going backwards, then here's how:
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
System.out.println("Last 7 days (starting today):");
Calendar cal = Calendar.getInstance(); // Initialized to today/now
for (int i = 0; i < 7; i++) {
System.out.println(" " + sdf.format(cal.getTime()));
cal.add(Calendar.DAY_OF_MONTH, -1); // Update to previous day at same time-of-day
}
OUTPUT
Last 7 days (starting today):
Monday
Sunday
Saturday
Friday
Thursday
Wednesday
Tuesday
This will subtract 7 days from the calendar to get you the date 7 days ago:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -7).
To subtract one day use the following code :
int DAY_IN_MILLIS = 1000 * 60 * 60 * 24;
Date currentDate = new Date();
long previousDay = currentDate.getTime()-DAY_IN_MILLIS;
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
String day = sdf.format(previousDay);

Add hours and Minutes to a certain time java

I have String initialTime= "10:30:00"
I am converting it into time like so:-
DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
Date date = sdf.parse(initialTime);
Time time = new Time(date.getTime());
int initHour = time.getHours()
int initMind = time.getMinutes();
Further I have two values;
int hour, mins;
The hour value can be anything from 0-10;
mins value can be 0,15,30,45.
The user selects a time by selecting hour and mins. As soon as the user selects the values they should get added to the initialTimeand shown in finalTIme
So if:-
hour=0 and mins=15 then finalTIme=10:45:00
hour=0 and mins=35 then finalTIme=11:00:00
hour=0 and mins=45 then finalTIme=11:15:00
I tried doing something like:-
if(hour==0 && mins==0)
{
finalTime = initialTime;
}
else if(hour==0 && mins>0 && mins <30)
{
mins = initMins + mins
}
else if(hour==0 && mins>0 && mins >=30)
{
hours = hours+1;
mins = mins-60;
}
But I did not get the required output. HOw can I do it in a less complicated manner?
You can use java.util.Calendar class:
DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
Date date = sdf.parse(initialTime);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR_OF_DAY, hours);
cal.add(Calendar.MINUTE, minutes);
date = cal.getTime();
You might also consider the new Date API from JDK 8.

How to get full date in android?

I know about to get the date in android with the help of the calender instance.
Calendar c = Calendar.getInstance();
System.out.println("====================Date is:"+ c.get(Calendar.DATE));
But with that i got only the number of the Date. . .
In My Application i have to do Some Calculation based on the Date Formate. Thus if the months get changed then that calculation will be getting wrong.
So for that reason i want the full date that gives the Month, Year and the date of the current date.
And what should be done if i want to do Some Calculation based on that date ?
As like: if the date is less then two weeks then the message should be printed. . .
Please Guide me in this.
Thanks.
Look at here,
Date cal=Calendar.getInstance().getTime();
String date = SimpleDateFormat.getDateInstance().format(cal);
for full date format look SimpleDateFormat
and IF you want to do calculation on date instance I think you should use, Calendar.getTimeInMillis() field on these milliseconds make calculation.
EDIT: these are the formats by SImpleDateFormat class.
String[] formats = new String[] {
"yyyy-MM-dd",
"yyyy-MM-dd HH:mm",
"yyyy-MM-dd HH:mmZ",
"yyyy-MM-dd HH:mm:ss.SSSZ",
"yyyy-MM-dd'T'HH:mm:ss.SSSZ",
};
for (String format : formats) {
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
}
EDIT: two date difference (Edited on Date:09/21/2011)
String startTime = "2011-09-19 15:00:23"; // this is your date to compare with current date
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = dateFormat.parse(startTime);
// here I make the changes.... now Date d use a calendar's date
Date d = Calendar.getInstance().getTime(); // here you can use calendar beco'z date is now deprecated ..
String systemTime =(String) DateFormat.format("yyyy-MM-dd HH:mm:ss", d.getTime());
SimpleDateFormat df1;
long diff = (d.getTime() - date1.getTime()) / (1000);
int Totalmin =(int) diff / 60;
int hours= Totalmin/60;
int day= hours/24;
int min = Totalmin % 60;
int second =(int) diff % 60;
if(day < 14)
{
// your stuff here ...
Log.e("The day is within two weeks");
}
else
{
Log.e("The day is more then two weeks");
}
Thanks.
Use SimpleDateFormat class,
String date = SimpleDateFormat.getDateInstance().format(new Date());
you can use
//try different flags for the last parameter
DateUtils.formatDateTime(context,System.currentTimeMillis(),DateUtils.FORMAT_SHOW_DATE);
for all options check http://developer.android.com/reference/android/text/format/DateUtils.html
try this,
int month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
int day = c.get(Calendar.DAY_OF_MONTH);
System.out.println("Current date : "
+ day + "/" + (month + 1) + "/" + year);
}
I'm using following methods to get date and time. You can change the locale here to arabic or wot ever u wish to get date in specific language.
public static String getDate(){
String strDate;
Locale locale = Locale.US;
Date date = new Date();
strDate = DateFormat.getDateInstance(DateFormat.DEFAULT, locale).format(date);
return strDate;
}
public static String getTime(){
String strTime;
Locale locale = Locale.US;
Date date = new Date();
strTime = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale).format(date);
return strTime;
}
you can get the value and save it on String as below
String Date= getDate();
String Time = getTime();

Categories