In android I recently had a task that involved converting a date string into a date object.i have many time used this type of conversion when we are used Date class and display it.
In this post i post the both conversion and many other combination.
String to Date useing Date Class:
String dtStart = "2012-10-15";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try{
Date date = format.parse(dtStart);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
Date to String useing Date Class:
SimpleDateFormat dformat= new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = new Date();
String datetime = dformat.format(date);
System.out.println("Current Date Time : " + datetime);
} catch (ParseException e) {
e.printStackTrace();
}
Now how to add any integer value in current/passed Date and used them.
you can add date two way..
(1)first is using Calander class.
Calendar c1 = Calendar.getInstance();
c1.add(Calendar.DAY_OF_MONTH, 1);
OR
Date dtStartDate=o.getStartDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar cal = Calendar.getInstance();
cal.setTime(dtStartDate);
cal.add(Calendar.DATE, 3); // number of days to add
String dt = sdf.format(c.getTime()); // dt is now the new date
In this post i post the both conversion and many other combination.
String to Date useing Date Class:
String dtStart = "2012-10-15";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try{
Date date = format.parse(dtStart);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
Date to String useing Date Class:
SimpleDateFormat dformat= new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = new Date();
String datetime = dformat.format(date);
System.out.println("Current Date Time : " + datetime);
} catch (ParseException e) {
e.printStackTrace();
}
Now how to add any integer value in current/passed Date and used them.
you can add date two way..
(1)first is using Calander class.
Calendar c1 = Calendar.getInstance();
c1.add(Calendar.DAY_OF_MONTH, 1);
OR
Date dtStartDate=o.getStartDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar cal = Calendar.getInstance();
cal.setTime(dtStartDate);
cal.add(Calendar.DATE, 3); // number of days to add
String dt = sdf.format(c.getTime()); // dt is now the new date