How to pass the date value of a dateChooserCombo to a variable - java

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);

Related

Format Date from string in one format back to string then parse string to date

Im trying to grab a string(date) parse with ("MM/dd/yyyy") then format date from the string with
("EEE MMM dd kk:mm:ss z yyyy") then parse that string again creating date with format ("EEE MMM dd kk:mm:ss z yyyy"). So I can compare two dates with format ("EEE MMM dd kk:mm:ss z yyyy")
Not getting any parsing errors but Im trying to use this to compare two dates. Running into issue with not being able to create list because my compare isnt working. Need a little help with this type of conversion
private Date getDateNDaysAgo(int numDays) {
log.debug("getDateNDaysAgo 1 " + numDays);
Calendar cal = Calendar.getInstance();
Date returnDate = null;
cal.add(Calendar.DATE, -(numDays));
returnDate = cal.getTime();
log.debug("getDateNDaysAgo 2" + returnDate);
return (returnDate);
}
String EffectiveHireString = null;
String EffectiveHireDate = null;
Date returnEffectiveHireDate = null;
Date thresholdDate = null;
Date newEffectiveHireDate=null;
boolean isAccountCreatedOver25Days = false;
link = (Link)itrLinkResults.next();
// only pull active accounts
if(!link.isDisabled()) {
if(link.getAttribute("lastLogon") != null){
log.debug("QueryParameter: lastLogon (1): not null");
lastLogon = link.getAttribute("lastLogon");
}
if(link.getAttribute("EffectiveHireDate") != null){
log.debug("QueryParameter: EffectiveHireDate (1): not null");
acuEffectiveHireDate = link.getAttribute("EffectiveHireDate");
if(Util.isNotNullOrEmpty(EffectiveHireDate)) {
DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy");
try {
newEffectiveHireDate = sdf.parse(EffectiveHireDate);
EffectiveHireString = df.format(newEffectiveHireDate);
returnEffectiveHireDate = formatter.parse(EffectiveHireString);
thresholdDate = getDateNDaysAgo(25);
log.debug("thresholdDate" + thresholdDate);
if(returnEffectiveHireDate.compareTo(thresholdDate) < 0){
log.debug("returnEffectiveHireDate 2" + returnEffectiveHireDate);
isAccountCreatedOver25Days = true;**
}
}
catch (ParseException e) {
log.error("Error attempting to parse EffectiveHireDate Date");
}
}
}
There is a conceptual problem with what you are trying to do. You are taking a date in the format MM/dd/yyyy and converting it to a string representation that has date, time, and time zone, but your initial data contains only date, not time or time zone. Of course, you can set the time to 00:00:00 and the time zone to your system default, but these are assumptions that may be incorrect.
In any case, here is how you could do it using the java.time API:
import java.time.*;
import java.time.format.*;
// . . .
DateTimeFormatter mdy = DateTimeFormatter.ofPattern("MM/dd/yyyy");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd kk:mm:ss z yyyy");
String date = "01/28/2022";
// the follow steps could be combined, but they are separated for clarity
LocalDate localDate = mdy.parse(date, LocalDate::from);
LocalDateTime ldt = LocalDateTime.of(localDate, LocalTime.of(0, 0));
ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault);
// formatted as "Fri Jan 28 24:00:00 EST 2022"
String formatted = formatter.format(zdt);
ZonedDateTime parsed = formatter.parse(formatted, ZonedDateTime::from);
// parsed.equals(zdt) == true
// parsed.compareTo(zdt) == 0
If you really just want to compare dates, I would strongly suggest using only the LocalDate class and leaving time and time zone out of it entirely.
Here is an example using only Date, Calendar, and (Simple)DateFormat:
boolean checkEffectiveHireDate(String hireDateMMddyyyy) throws ParseException {
DateFormat mmddyyyy = new SimpleDateFormat("MM/dd/yyyy");
DateFormat fullFormat = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy");
Date hireDate = mmddyyyy.parse(hireDateMMddyyyy);
String effectiveHire = fullFormat.format(hireDate);
System.out.println(effectiveHire);
Date effectiveHireDate = fullFormat.parse(effectiveHire);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -25);
Date thresholdDate = cal.getTime();
return effectiveHireDate.compareTo(thresholdDate) < 0;
}

Set date and desired time in Android

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

Setting Date in google calendar event

I am trying to set start date of an event in google calendar but it keeps taking the wrong value.
The code
EventDateTime start = new EventDateTime();
EventDateTime end = new EventDateTime();
SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-DD'T'hh:mm:ss.SSS'Z'");
Date dt = new Date();
Date endDate = new Date();
dt = df.parse("2015-05-23T09:00:00.000Z");
endDate = df.parse("2015-05-25T09:30:00.000Z");
start.setDateTime(new DateTime(dt));
end.setDateTime(new DateTime(endDate));
But the variables 'end' and 'start' take the values "2014-12-28T09:30:00.000+05:30 " and "2014-12-28T09:00:00.000+05:30" respectively.
My Timezone is GMT+5:30 .
Because it should be: yyyy-MM-dd not YYYY-MM-DD
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");
here you can find what symbols int SimpleDateFormat means:
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
and th big DD is day of the year, and the small dd is day of the month.

Add a value to time

How to get system date and time.After getting time add 4hour to that time.Time Format is 12 Hour.I tried Like this
enter code here
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dataFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss a");
String strTime = dataFormat.format(calendar.getTime());
calendar.add(Calendar.HOUR_OF_DAY, 4);
Example:I given 10.30AM add 4hour.I need 2.30PM
Try this..
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR_OF_DAY, 4);
SimpleDateFormat dataFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
String strTime = dataFormat.format(calendar.getTime());

How to parse "dd-MM" date format to get current year?"

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

Categories

Resources