I am using Eclipse to do a college project on Java. I realized that java does not have a built in date selector like C#, so I downloaded and added JDateChooser. I tried to retrieve the chosen date but it failed:
String Date = dateChooser.getDate(); //I want to the date to be retrieved as string
Any ideas? Is there some kind of initialization that I must do?
Retrieve the date that the user selected by calling getDate(), which returns a Date object. Then convert that object into a String by calling SimpleDateFormat.format():
Date d;
SimpleDateFormat sdf;
String s;
d = dateChooser.getDate(); // Date selected by user
sdf = SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); // Or whatever format you need
s = sdf.format(d); // Viola
See also: Question 5683728
If I have the right component, the Java Docs show that the getDate method returns Date
getDate
public java.util.Date getDate() Returns the date. If the
JDateChooser is started with a null date and no date was set by the
user, null is returned. Returns: the current date
String dob =new SimpleDateFormat("dd-MMM-yyyy").format(jDateChooser1.getDate());
Related
I have a ProfileFragment where I want to show the user's profile. I also added the birth date in Firestore and now I want to display it for the current user on his profile so, how can I do that? Do I have to convert it to a string or what should I do ? I tried to make it a string with .toString() but it isn't displayed.If I let it like that or make it a Date it will show me an error when I try to "setText" to the TextView variable.
TVbirthdate = getActivity().findViewById(R.id.birthdateinfo);
Timestamp birthResult = task.getResult().getTimestamp("Birth Date");
TVbirthdate.setText(birthResult.toDate());
Thanks!
You have to convert the timestamp to the actual date format first before using it on the setText() method. See sample converter function below:
private String getDate(long time) {
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(time);
String date = DateFormat.format("dd-MM-yyyy", cal).toString();
return date;
}
After converting, you can now set this date using the setText() method. See sample code below:
// birthResult here is the Timestamp object from Firestore
TVbirthdate.setText(getDate(birthResult));
If the above code is not possible for your use-case, you can also use SimpleDateFormat. See sample implementation below:
Date dataDate = birthResult.toDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
TVbirthdate.setText(sdf.format(dataDate));
The user needs to select any date (Userdate). I need to set another jdate one day behind this date (needdate). Can you help with this?
Date date1 = (Userdate.getDate(),-1);
needdate.setDate(date1)
The reason why everyone is telling you to use the new java.time package classes like LocalDate and LocalDateTime etc is because Date is quite old and known to be error prone. But hey...if you're bent on using Date then I suppose you can do it this way:
// Get the date from the JDateChooser
Date date = jDateChooser1.getDateEditor().getDate();
// Subtract one day from the selected JDateChooser date.
Date oneDayFromSelectedDate = new Date(date.getTime() - Duration.ofDays(1).toMillis());
// Define the format you want the Date String;
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
// Dump it into a String variable and...
String myDateString = sf.format(oneDayFromSelectedDate);
// Display it into the Console Window
System.out.println(myDateString);
The oneDayFromSelectedDate is the Date object containing the JDateChooser selected date less one day. Do consider changing to the java.time package.
i have one Date field to save date into DB and date field should be in Date and Time format (MM/dd/yyyy HH:mm:ss)..
for that i am using DateFormat to convert the current Date as,
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date date = new Date();
String DateStr = dateFormat.format(date);
Here the result will be in String format and to save as Date object in DB using Hibernate i have to again convert that date string into Date obj as,
Date newdate = dateFormat.parse(DateStr);
So my question was, is there any better way to return the current Date along with Time as Date obj..
and also does Hibernate will automatically convert the String to Date, if we set the field type as String and by annotating as,
#Temporal(TemporalType.DATE)
#Column(name = "REQUESTED_DATE")
public String getRequestedDate() {
return requestedDate;
}
thanks.
Change your annotation to:
#Temporal(TemporalType.TIMESTAMP)
This will persist the time to the DB.
You can use java.sql.Date to store values that map to SQL DATETIME (or the MySQL variant). I do not understand why you say "by using new Date(),i [sic] will be getting only date not time". That is not correct. However, it is not optimal. You can also use java.sql.Timestamp. You get the best results when you use java.util.Calendar.
I found http://www.developerscrappad.com/228/java/java-ee/ejb3-jpa-dealing-with-date-time-and-timestamp/ in less than five minutes of Googling, and it answers your question pretty well.
#Temporal(value = TemporalType.TIMESTAMP)
Date date;
you can store date object directly into database that hibernate offers you. So, lets get rid of string.
I have date saletime as 2/25/14 22:06 I want to store it in oracle table in the yyyy-MM-dd hh:mm:ss. So I wrote following java code
Date saleTime = sale.getSaleTime();
logger.info("DateTime is "+saleTime);
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date saleTimeNorm = formatter.parse(formatter.format(saleTime));
logger.info("DateTime after Formating "+saleTimeNorm);
Timestamp oracleDate = new Timestamp(saleTimeNorm.getTime());
logger.info("New Format Inserting :"+oracleDate);
sale.setSaleTime(oracleDate);
But this seems to be giving :0014-02-25 22:06:00.0
Any suggestions ?
Your getSaleTime() method somehow regards "14" as a four-digit year, and returns the year 14.
After you have executed getSaleTime(), you already have a Date variable; there is no need (and no use) in converting it to a different output format and re-parsing the result. The Date you get from the calls to format() and parse() will be the same one you started with.
You can create your Timestamp using getTime() on the result of the call to getSaleTime(). That will be correct once you change getSaleTime() so that it returns the date in the correct year.
Something must be wrong in your sale.getSaleTime() method. Because the following code working as needed.
Date saleTime = Calendar.getInstance().getTime();
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date saleTimeNorm = formatter.parse(formatter.format(saleTime));
Timestamp oracleDate = new Timestamp(saleTimeNorm.getTime());
System.out.println(oracleDate);
//2014-05-13 03:58:53.0
I have to get a Date in type Date, not in String.
I have this code:
DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
Date date1 = new Date();
String date = (formatter.format(date1));
// At this point I get the date in correct format i.e 05/24/11
Date todaysDate = (Date)formatter.parse(date);
// But after this I get the date in format : Tue May 24 00:00:00 EDT 2011
// whereas I Want to get the date like above i.e 05/24/11
// And in type Date, not in type String
If anyone could help, thanks
The Date object just represents a point in time and has no notion of a format (or time zone). If you print out a Date object it first converts it to a String using the default formatting of EEE MMM dd HH:mm:ss zzz yyyy. If you want a specific formatting when you print it or otherwise represent it as a String, you'll need to use a formatter just like you already have.
In other words, you want Date.toString() to return the same as DateFormat.format()? You could just do exactly that:
public class MyDate extends Date {
DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
public String toString() {
return this.formatter.format(this);
}
}
But do you really want to mix up presentation (date format) with your data?
There is no problem here, you have a Date representing and can save it into the DB as it is now. If you print it to the console it gets formatted according the default rules, this is why you think it is different from what you need, but it has actually already the right value.
So just go ahead and put it into your DB.
Chances are that you DB will hold on getting a Timestamp, in this case you can create one:
Date d = ...
java.sql.Timestamp ts = new java.sql.Timestamp(d.getTime());
and save this one.