計算50天如何在Andriod的日期選擇器中輸入的天計算50天。在日期選擇器
例如,如果用戶選擇23/02/2013那麼什麼WLD是從第50天THT XX/XX/XXXX同樣100天第200天像明智的第1000天,請幫我在這邏輯
計算50天如何在Andriod的日期選擇器中輸入的天計算50天。在日期選擇器
例如,如果用戶選擇23/02/2013那麼什麼WLD是從第50天THT XX/XX/XXXX同樣100天第200天像明智的第1000天,請幫我在這邏輯
至於我明白你的問題,你可以嘗試做這樣的事情:
// this const is 24 hours in milliseconds: 24 hours in day, 60 min. in one hour, 60 sec. in one min., 1000 ms. in one sec.
private static final int TWENTY_FOUR_HOURS = 24 * 60 * 60 * 1000;
//...
Date dateYouChose;
Date dateYouChosePlus50Days = new Date(dateYouChose.getTime() + (50 * TWENTY_FOUR_HOURS));
更新時間:
所以,也許這將是對你有好處(但要小心,我沒有測試此代碼,也許我犯了一個錯誤,我n東西):
final DatePicker datePicker;
final Button btnDisplayCelebrationTimes;
final TextView txtDatePlus50;
final TextView txtDatePlus100;
// ...
btnDisplayCelebrationTimes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
final GregorianCalendar gregorianCalendar;
gregorianCalendar = new GregorianCalendar(datePicker.getYear(), datePicker.getMonth(),
datePicker.getDayOfMonth());
gregorianCalendar.add(Calendar.DAY_OF_MONTH, 50);
final Date datePlus50 = gregorianCalendar.getTime();
txtDatePlus50.setText(dateFormat.format(datePlus50));
gregorianCalendar = new GregorianCalendar(datePicker.getYear(), datePicker.getMonth(),
datePicker.getDayOfMonth());
gregorianCalendar.add(Calendar.DAY_OF_MONTH, 100);
final Date datePlus100 = gregorianCalendar.getTime();
txtDatePlus100.setText(dateFormat.format(datePlus100));
}
});
使用下面的簡單代碼來動態添加天數。
int Days = 50; // this is the input . you can change it in argument. the format of the date also changeable as per your requirement.
try
{
Date date = null;
String str = "2013-06-04";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
date = formatter.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE,Days);
System.out.println("" + formatter.format(calendar.getTime()));
}
catch (Exception e)
{
e.printStackTrace();
}
希望這可以幫助你。
編輯。
你好,這是完整的工作代碼。
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class MyAndroidAppActivity extends Activity {
private TextView tvDisplayDate;
private DatePicker dpResult;
private Button btnChangeDate;
private int year;
private int month;
private int day;
static final int DATE_DIALOG_ID = 999;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setCurrentDateOnView();
addListenerOnButton();
}
// display current date
public void setCurrentDateOnView() {
tvDisplayDate = (TextView) findViewById(R.id.tvDate);
dpResult = (DatePicker) findViewById(R.id.dpResult);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
tvDisplayDate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
// set current date into datepicker
dpResult.init(year, month, day, null);
}
public void addListenerOnButton() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month,
day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
String str= year+"-"+(month+1)+"-"+day;
System.out.println(" rrrrrr " +str);
tvDisplayDate.setText(str); // this is the current date.
tvDisplayDate.setText(" Old Date" + str + "After adding 50 days" +dateadding(50, str));
// set selected date into datepicker also
dpResult.init(year, month, day, null);
}
};
String dateadding(int DaystoAdd,String Date)
{
int Days = DaystoAdd; // this is the input . you can change it in argument. the format of the date also changeable as per your requirement.
String finaldate="";
try
{
Date date = null;
String str = Date;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
date = formatter.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE,Days);
finaldate= formatter.format(calendar.getTime());
}
catch (Exception e)
{
e.printStackTrace();
}
return finaldate;
}
}
設計線條
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnChangeDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Date" />
<TextView
android:id="@+id/lblDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Date (M-D-YYYY): "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<DatePicker
android:id="@+id/dpResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
我試過兩個它不起作用的代碼。 – Varun
對我來說它工作得很好。由於我使用的日期選擇器我打電話裏面DatePickerDialog.OnDateSetListener – itsrajesh4uguys
? – Varun
您好我需要創建一個名爲 '時間來慶祝' 的應用程序。 1.獲取用戶的日期輸入。 (日期可能是一些生日,或者婚姻,或者一些重要的事件) 2.點擊按鈕文本「顯示慶典時間」,那麼它應該顯示從給定日期 一以下。第50天(例如,如果給定的日期是2013年3月6日,然後加入50天並顯示第50天將是爲xxxx-xxxx) 灣第100,第200,第1,第500,1000至2000年至第5000日 我有一個疑問,你上面的代碼dateYouChose不應該是一個datepicker的obj,因爲我從userthrodatepicker – Varun
看到我的更新回答 –