My input is
String a1="2003-08-15";
I want to set a1 in a pojo class of type Calendar..
Here is my code..
String a1="2003-08-15";
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date date = formatter.parse(a1);
Calendar calender = Calendar.getInstance();
calender.setTime(date);
item.setDate1(calender);
My output looks like this: "0021-01-22 12:30:00"
Is this correct output??
Try like this
String a1="2003-08-15";
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cal.setTime(sdf.parse(a1));
Date date=cal. getTime();
System.out.println(date);
I want to set date and time in my android app. The date should be today's date but the time should be set to 6:00 AM by default in the text field. I have read many links but most of them shows today's time and date (example: 2016-03-28 11:53:55).
String timetxt1 = "06:00:00";
Date datetxt1 = null;;
try {
datetxt1 = simpleDateFormat.parse(timetxt1);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar selectedDate1 = Calendar.getInstance();
selectedDate1.setTime(datetxt1);
edittxt.setText(dateFormatter.format(selectedDate1.getTime()));
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 6);// for 6 hour
calendar.set(Calendar.MINUTE, 0);// for 0 min
calendar.set(Calendar.SECOND, 0);// for 0 sec
System.out.println(calendar.getTime());// print 'Mon Mar 28 06:00:00 ALMT 2016'
This was useful to me.
fun getFormattedDateTime(dateString: String):String{
var formattedDate=""
val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault())
val dateFormat = SimpleDateFormat("dd/MM/yyyy", Locale.getDefault())
val calendar = Calendar.getInstance()
calendar.time = dateFormat.parse(dateString)!!
calendar[Calendar.HOUR_OF_DAY]=6
calendar[Calendar.MINUTE]=0
calendar[Calendar.SECOND]=0
formattedDate=sdf.format(calendar.time)
return formattedDate
}
To get current date use below method
public static String getCurrentDate() {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd", Locale.getDefault());
Date date = new Date();
return dateFormat.format(date);
}
This will give you the current date alone. And if you want the time to be 6:00AM use the below method
public static String getCurrentDateAndTime6() {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault());
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE,0);
return dateFormat.format(calendar);
}
I've done like this,
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
date.setHours(6);
date.setMinutes(0);
date.setSeconds(0);
Log.d("DateTime", dateFormat.format(date));
OUTPUT : 2016/03/28 06:00:00
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy",Locale.ENGLISH);
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.setTime(sdf.parse(dateChooserCombo1.getSelectedDate().toString()));//code to select date from the dateChooserCombo and Parse as string
int x = Integer.parseInt(txtDays.getText());
c.add(Calendar.DATE, x);
SimpleDateFormat print = new SimpleDateFormat("yyyy/MM/dd");
txtEndDate.setText(print.format(c.getTime()));
I'm trying to selected a Startdate from the dateChooserCombo and increment it by the number of days required to complete a course and then write the endDate to a Textfield. If I use today date as shown below the code works fine.
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy",Locale.ENGLISH);
Calendar c = Calendar.getInstance();
c.setTime(new Date());
//c.setTime(sdf.parse(dateChooserCombo1.getSelectedDate().toString()));
int x = Integer.parseInt(txtDays.getText());
c.add(Calendar.DATE, x);
//SimpleDateFormat print = new SimpleDateFormat("yyyy/MM/dd");
txtEndDate.setText(sdf.format(c.getTime()));
Can someone please tell me where am I going wrong or how to pass a dateChooserCombo value to a date variable which I can manipulate?
Calendar c = Calendar.getInstance();
c.setTime(new Date());
txtEndDate.setSelectedDate(c);
I have to parse "17-Jun" format date using Java.But the problem is when I try to parse "dd-MM" format using SimpleDateFormat it is returning as "Wed Jun 17 00:00:00 IST 1970".Is it possible to get current(2014) year instead of 1970.
My result:
17/JUNE/1970
Expected result:
17/JUNE/2014
Have a look at this..
Calendar c = Calendar.getInstance();
c.set(Calendar.DATE, 17);
c.set(Calendar.MONTH, 5);
c.set(Calendar.YEAR, c.get(Calendar.YEAR));
Date date=new Date(c.getTimeInMillis());
SimpleDateFormat simpleDateformatter = new SimpleDateFormat("dd/mmm/yyyy");
String convertedDate = simpleDateformatter .format(date);
To get year you can just use
Calendar cal = Calendar.getInstance();
cal.get(Calendar.YEAR) will fetch you current year
Hope it helped... :)
Try this
Calendar c = Calendar.getInstance();
c.set(Calendar.DATE, 17);
c.set(Calendar.MONTH, 5);
c.set(Calendar.YEAR, c.get(Calendar.YEAR));
Date d=new Date(c.getTimeInMillis());
SimpleDateFormat formatter = new SimpleDateFormat("dd- mmm");
String conDate = formatter.format(d);
Do like this
Date date = new SimpleDateFormat("dd-MMM-yyyy").parse("17-Jun-"+ Calendar.getInstance().get(Calendar.YEAR));
You'll have to write a utility method, there isn't anything in SimpleDateFormat that will interpret a non-existant year as the current year. Something like this:
public static Date parseDate(String dateString) throws ParseException {
//determine current year
Calendar today = Calendar.getInstance();
int currentYear = today.get(Calendar.YEAR);
//parse input
SimpleDateFormat format = new SimpleDateFormat("dd-MMM");
Date parsed = format.parse(dateString);
// set current year on parsed value
Calendar cal = Calendar.getInstance();
cal.setTime(parsed);
cal.set(Calendar.YEAR, currentYear);
return cal.getTime();
}
Try this:
SimpleDateFormat dfDate = new SimpleDateFormat("dd-MM-yyyy");
java.util.Date d = null;
try {
d = dfDate.parse("17-Jun-"+ Calendar.getInstance().get(Calendar.YEAR));
} catch (java.text.ParseException e) {
e.printStackTrace();
}
System.out.println(""+d );
your problem will be solved.
java.time
In Java 8 you can do something like:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("d-MMM");
MonthDay md = MonthDay.parse("17-Jun", dtf);
LocalDate d = LocalDate.now().with(md);
System.out.println(d.getDayOfMonth());
System.out.println(d.getMonthValue());
System.out.println(d.getYear());
I guess the simplest way is to do this:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MMM/dd");
Date date = new Date();
System.out.println("Time is: " + dateFormat.format(date) );
This gives you exactly what you want. also see
http://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html
Little late, but if you really don't want to use Calendar at all - as I gather from your comments to the correct answers above - (not recommended with the usage of deprecated methods, but still):
SimpleDateFormat format = new SimpleDateFormat("dd-MMM");
Date date = format.parse("17-JUN");
date.setYear(new Date().getYear());
System.out.println(date);
Output:
Tue Jun 17 00:00:00 IST 2014
All answers given here are more or less correct, but I notice that one detail aspect is still overlooked, namely if the combination of day and months fits to current year (february 29 problem). So I would suggest a strict parsing like following:
String ddMMM = "17-Jun";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
sdf.setLenient(false); // in order to check for "29-Feb"
TimeZone tz = TimeZone.getDefault(); // or change to your specific time zone
Date date =
sdf.parse(ddMMM + "-" + new GregorianCalendar(tz).get(Calendar.YEAR));
Try,
String s2 = "Wed Jun 17 00:00:00 1970";
SimpleDateFormat sdf1 = new SimpleDateFormat("E MMM dd hh:mm:ss yyyy");
SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MMM/yyyy");
try {
Date d1 = sdf1.parse(s2);
System.out.println(d1);
String s3 = sdf2.format(d1);
System.out.println("Before Changing :: "+s3);
Calendar cal = Calendar.getInstance();
cal.setTime(d1);
cal.add(Calendar.YEAR, 2014-1970);
d1 = cal.getTime();
String s4 = sdf2.format(d1);
System.out.println("After Changing :: "+s4);
} catch (ParseException e) {
e.printStackTrace();
}
Output
Before Changing :: 17/Jun/1970
After Changing :: 17/Jun/2014
I got a list of dates as String
for example date1->'11-11-2010' and date2->'12-01-2011'
I want to print all the dates between these two dates..
I tried to work with cal.add() but am not able to set my date1 to my cal.. if i do so i get null p
below code should do the trick for you.
String date1 = "11-11-2010";
String date2 = "12-01-2011";
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(format.parse(date1));
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(format.parse(date2));
Date currentDate = calendar1.getTime();
while(!currentDate.equals(cal2.getTime())){
calendar1.add(Calendar.DAY_OF_MONTH, 1);
currentDate = cal1.getTime();
System.out.println(currentDate);
}