【发布时间】:2012-02-03 00:48:10
【问题描述】:
在我的数据库中,我得到像 2011-11-30(yyyy/mm/dd) 格式的开始日期和像 40 天这样的持续时间。我如何计算天数并获得 mm/dd/yyyy 的新日期格式.
谁能帮帮我
谢谢
【问题讨论】:
-
寻找java的日期函数
在我的数据库中,我得到像 2011-11-30(yyyy/mm/dd) 格式的开始日期和像 40 天这样的持续时间。我如何计算天数并获得 mm/dd/yyyy 的新日期格式.
谁能帮帮我
谢谢
【问题讨论】:
这个问题的现代答案姗姗来迟。请考虑将现代 Java 日期和时间 API java.time 用于您的日期工作。
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);
输出是:
2012 年 1 月 9 日
java.time 在较旧和较新的 Android 设备上都能很好地工作。它只需要至少 Java 6。
org.threeten.bp 导入日期和时间类。java.time。java.time 的反向移植到 Java 6 和 7(JSR-310 的 ThreeTen)。【讨论】:
不要使用:
Calendar c = Calendar.getInstance();
改用这个:
GregorianCalendar calendar =new GregorianCalendar();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.DAY_OF_MONTH , 30);
【讨论】:
这段代码应该能很好地完成这项工作!!!
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
【讨论】:
oldDate in c.setTime(dateFormat.parse(oldDate)); 是一个日期,函数需要一个日期。
它对我有用。
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);
【讨论】:
只需简单复制此方法传递数据
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");
【讨论】:
此代码 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);
【讨论】:
你可以试试这样的,
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());
【讨论】:
SimpleDateFormat 和 Calendar。这些类设计不良且过时,尤其是前者,尤其是出了名的麻烦。而是使用来自java.time, the modern Java date and time API 的LocalDate 和DateTimeFormatter。
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();
【讨论】:
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);
【讨论】:
Step-1 从指定字符串中获取日历实例
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dateInString));
第二步使用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);
【讨论】: