在我的数据库中,我的开始日期如2011-11-30(yyyy / mm / dd)格式。持续时间为40天。如何计算天数并获得mm / dd / yyyy的新日期格式
任何人都可以帮助我
谢谢
答案 0 :(得分:71)
您可以尝试这样的事情,
String dt = "2012-01-04"; // Start date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
try {
c.setTime(sdf.parse(dt));
} catch (ParseException e) {
e.printStackTrace();
}
c.add(Calendar.DATE, 40); // number of days to add, can also use Calendar.DAY_OF_MONTH in place of Calendar.DATE
SimpleDateFormat sdf1 = new SimpleDateFormat("MM-dd-yyyy");
String output = sdf1.format(c.getTime());
答案 1 :(得分:21)
Step-1 从指定字符串中获取日历实例
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dateInString));
第2步使用add()
为日历添加天数
c.add(Calendar.DATE, 40);
步骤3 将dtae转换为结果日期格式
sdf = new SimpleDateFormat("MM/dd/yyyy");
Date resultdate = new Date(c.getTimeInMillis());
dateInString = sdf.format(resultdate);
源代码
String dateInString = "2011-11-30"; // Start date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dateInString));
c.add(Calendar.DATE, 40);
sdf = new SimpleDateFormat("MM/dd/yyyy");
Date resultdate = new Date(c.getTimeInMillis());
dateInString = sdf.format(resultdate);
System.out.println("String date:"+dateInString);
答案 2 :(得分:7)
Date dtStartDate=o.getStartDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar c = Calendar.getInstance();
c.setTime(dtStartDate);
c.add(Calendar.DATE, 3); // number of days to add
String dt = sdf.format(c.getTime()); // dt is now the new date
Toast.makeText(this, "" + dt, 5000).show();
答案 3 :(得分:3)
此代码100%正常工作
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss");
String reg_date = df.format(c.getTime());
showtoast("Currrent Date Time : "+reg_date);
c.add(Calendar.DATE, 3); // number of days to add
String end_date = df.format(c.getTime());
showtoast("end Time : "+end_date);
答案 4 :(得分:2)
这段代码应该可以很好地完成工作!
public static String addDay(String oldDate, int numberOfDays) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
try {
c.setTime(dateFormat.parse(oldDate));
} catch (ParseException e) {
e.printStackTrace();
}
c.add(Calendar.DAY_OF_YEAR,numberOfDays);
dateFormat=new SimpleDateFormat("MM-dd-YYYY");
Date newDate=new Date(c.getTimeInMillis());
String resultDate=dateFormat.format(newDate);
return resultDate;
}
有关更多功能,请查看此链接
Add days,months,years to a date
答案 5 :(得分:1)
Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
// display the current date
String CurrentDate = mYear + "/" + mMonth + "/" + mDay;
String dateInString = CurrentDate; // Start date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
c = Calendar.getInstance();
try {
c.setTime(sdf.parse(dateInString));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c.add(Calendar.DATE, 7656);//insert the number of days you want to be added to the current date
sdf = new SimpleDateFormat("dd/MM/yyyy");
Date resultdate = new Date(c.getTimeInMillis());
dateInString = sdf.format(resultdate);
//Display the Result in the Edit Text or Text View your Choice
EditText etDOR = (EditText)findViewById(R.id.etDateOfReturn);
etDOR.setText(dateInString);
答案 6 :(得分:1)
这个问题的现代答案早就该了。在您的约会工作中,请考虑使用现代Java日期和时间API java.time。
for row in query_res:
return row[0]
输出为:
2012年1月9日
java.time在旧的和更新的Android设备上均可正常运行。它只需要至少 Java 6 。
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("MM/dd/uuuu");
int durationDays = 40;
LocalDate startDate = LocalDate.parse("2011-11-30");
LocalDate newDate = startDate.plusDays(durationDays);
String formattedDate = newDate.format(outputFormatter);
System.out.format(formattedDate);
导入日期和时间类。org.threeten.bp
。java.time
向Java 6和7(JSR-310的ThreeTen)的反向端口。答案 7 :(得分:0)
只需简单复制此方法传递数据
public static String ConvertToDate(String dateString,String inputFormater ,String outputFormater) {
String outputText = "" ;
try {
SimpleDateFormat inputFormat = new SimpleDateFormat(inputFormater);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputFormater);
Date parsed = null;
parsed = inputFormat.parse(dateString);
outputText = outputFormat.format(parsed);
Log.d(TAG, " msg : " + outputText);
} catch (ParseException e) {
e.printStackTrace();
Log.e(TAG, "ERROR : " + e.getMessage());
}
return outputText;
}
并称之为
public static String INPUTE_JSON_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static String FORMAT_FOR_JOB_APPLIED = "MMM dd, yyyy";
String date ="2017-10-29";
ConvertToDate(date ,DateUtils.INPUTE_JSON_DATE_FORMAT ,DateUtils.FORMAT_FOR_JOB_APPLIED);
您可以在录音中添加任何您想要的格式
ConvertToDate("2011-11-30","yyyy/mm/dd" ,"mm/dd/yyyy");
答案 8 :(得分:0)
它对我有用。
Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
String CurrentDate = mYear + "/" + mMonth + "/" + mDay;
String dateInString = dob; // Select date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
c = Calendar.getInstance();
try {
c.setTime(sdf.parse(dateInString));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c.add(Calendar.DATE, 14); //14 dats add
sdf = new SimpleDateFormat("dd/MM/yyyy");
Date resultdate = new Date(c.getTimeInMillis());
dateInString = sdf.format(resultdate);
System.out.println(dateInString);
答案 9 :(得分:0)
请勿使用:
Calendar c = Calendar.getInstance();
改为使用此:
GregorianCalendar calendar =new GregorianCalendar();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.DAY_OF_MONTH , 30);