In my database I am getting start date like 2011-11-30(yyyy/mm/dd)format.and duration date like 40 days.How can i calculate the days and get new date format of mm/dd/yyyy.
在我的数据库中,我的开始日期如2011-11-30(yyyy / mm / dd)格式。持续日期为40天。如何计算天数并获得mm / dd / yyyy的新日期格式。
Can anyone help me
谁能帮我
Thanks
8 个解决方案
#1
64
You can try something like this,
你可以尝试这样的事情,
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());
#2
19
Step-1 Get Calendar instance from the specified string
步骤1从指定的字符串中获取Calendar实例
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dateInString));
Step-2 use add()
to add number of days to calendar
步骤2使用add()向日历添加天数
c.add(Calendar.DATE, 40);
Step-3 Convert the dtae to the resultant date format
步骤3将dtae转换为结果日期格式
sdf = new SimpleDateFormat("MM/dd/yyyy");
Date resultdate = new Date(c.getTimeInMillis());
dateInString = sdf.format(resultdate);
Source Code
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);
#3
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();
#4
2
This code is working 100%
此代码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);
#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
0
Just Simple copy this method pass data
只需简单复制此方法传递数据
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;
}
And call it like
并称之为
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);
You can put any format you want in your cituation
你可以在你的cituation中放置你想要的任何格式
ConvertToDate("2011-11-30","yyyy/mm/dd" ,"mm/dd/yyyy");
#7
0
It works for me.
这个对我有用。
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);
#8
0
This piece of code should do the job well!!!
这段代码应该可以很好地完成工作!
public static Date addDay(Date 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);
}
For more functionality do please checkout this link
Add days,months,years to a date
有关更多功能,请查看此链接将日期,月份,年份添加到日期
#1
64
You can try something like this,
你可以尝试这样的事情,
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());
#2
19
Step-1 Get Calendar instance from the specified string
步骤1从指定的字符串中获取Calendar实例
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dateInString));
Step-2 use add()
to add number of days to calendar
步骤2使用add()向日历添加天数
c.add(Calendar.DATE, 40);
Step-3 Convert the dtae to the resultant date format
步骤3将dtae转换为结果日期格式
sdf = new SimpleDateFormat("MM/dd/yyyy");
Date resultdate = new Date(c.getTimeInMillis());
dateInString = sdf.format(resultdate);
Source Code
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);
#3
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();
#4
2
This code is working 100%
此代码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);
#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
0
Just Simple copy this method pass data
只需简单复制此方法传递数据
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;
}
And call it like
并称之为
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);
You can put any format you want in your cituation
你可以在你的cituation中放置你想要的任何格式
ConvertToDate("2011-11-30","yyyy/mm/dd" ,"mm/dd/yyyy");
#7
0
It works for me.
这个对我有用。
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);
#8
0
This piece of code should do the job well!!!
这段代码应该可以很好地完成工作!
public static Date addDay(Date 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);
}
For more functionality do please checkout this link
Add days,months,years to a date
有关更多功能,请查看此链接将日期,月份,年份添加到日期