I'm working with the Vaadin Framework at the moment. I want to convert the string in my DateField to Date. So I have two classes, one is the view and the other should contain the values which I save with data binding.
This is the DateField in the view:
timestart = new DateField("");
timestart.setId("timestart");
timestart.setDateFormat("yyyy-MM-dd HH:mm");
timestart.setValue(new Date());
timestart.setResolution(Resolution.MINUTE);
timestart.setConverter( XXX ); // Here i don't know what to do
layout.addComponent(timestart, 3, 2);
In the same class the data binding:
binder.bind(timestart, "timestart");
//This part is working
And in my other class:
private Date timestart;
I want to save this timestart in a database, so i need a formatted value like above yyyy-MM-dd HH:mm but when I do it without timestart.setConverter I am getting a date like Wed Jul 16 11:11:00 CEST 2014.
How should I do this ?
You need to format the Date in your bean class. Not in your View code.
private SimpleDateFormat dateFormat;
private Date timestart;
public ConstructorOfYourClass{
timestart = new Date(); //Default date
//Your prefered date format
dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
}
//... other Code ...
//Getter method of your Date
public String getdateFormat(){
return dateFormat.format(timestart);
}
Related
I am trying to parse the date to look like 03-23-2015 21:16:00 GMT+05:00 using joda-time but i am not able to achieve it, however it is working fine with SimpleDateFormat but for some reason i want to use Joda-Time (see my question on SO.)
Please note that i don't want to hardcode timezone to GMT+05:00 but i want to set the user's default timezone.
I am trying it as:
public class Consts{
public static final DateTimeFormatter DATE_FORMATTER_2 = DateTimeFormat
.forPattern("MM-dd-yyyy HH:mm:ss z");
public static final DateTimeFormatter DATE_FORMATTER_TEMP_1 = DateTimeFormat
.forPattern("MM-dd-yyyy HH:mm:ss Z");
}
And then i am using these formatters as:
cDate = new LocalDateTime(DateTimeZone.getDefault());
sDate = new LocalDateTime(DateTimeZone.getDefault());
eDate = new LocalDateTime(DateTimeZone.getDefault());
if (mStartTimeTV.getText().toString().equals("Now")) {
sDate = cDate;
} else {
sDate = Consts.DATE_FORMATTER_WITHOUT_TIME_ZONE
.parseLocalDateTime(mStartTimeTV.getText().toString());
}
if (!mEndTimeTV.getText().toString().equals("")) {
eDate = Consts.DATE_FORMATTER_WITHOUT_TIME_ZONE
.parseLocalDateTime(mEndTimeTV.getText().toString());
} else {
eDate = sDate;
}
And while sending the dates to the server i am formatting them as:
String s0 = Consts.DATE_FORMATTER_2.print(sDate);
String s = Consts.DATE_FORMATTER_2.withZone(
DateTimeZone.getDefault()).print(sDate);
String s1 = Consts.DATE_FORMATTER_TEMP_1.print(sDate);
But the output is always: 03-24-2015 16:07:23
I have also tried with ZZZZ but no luck.
LocalDateTime doesn't have a time zone, so there is nothing to format. You should use DateTime instead, which you can obtain using LocalDateTime.toDateTime(timeZone).
Is it possible to do format a date of type String using a date formatter? I want to store my Date and Time in the Event class as Strings so that I don't need to convert the Strings loaded from a MYSQL database (using the types DATE and TIME) back into Date types so they can be stored in new Event objects. MySQL only accepts DATE in the format of YYYY-MM-DD and TIME in the format of HH:MM:SS but i want these to be formatted differently when i go to print them out in my program.
When i run this code i get an Cannot format given Object as a Date at java.text.DateFormat.format(Unknown Source) error. If i try using parse() it won't compile because it only accepts Dates.
Main class
public Main() {
ArrayList<Event> events = new ArrayList<Event>();
private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:MM:SS");
private SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD");
//Stores current time and date
Date date;
Date time;
String d = "";
String t = "";
d= dateFormat.parse(date);
t= timeFormat.parse(time);
events.add(d, t);
//Print out newly formatted date and time when loaded from mysql
System.out.println(events.get(0).printDate());
System.out.println(events.get(0).printTime());
}
Events class
public class Event {
private String date;
private String time;
public Event(String d, String t) {
date = d;
time = t;
}
public String printDate() {
SimpleDateFormat format = new SimpleDateFormat("DD/MM/YYYY");
String newDate = format.format(date);
return newDate;
}
public String printTime() {
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
String newTime = format.format(time);
return newTime;
}
}
In Event, you should use Date type for date and time field.
This is a more appropriate representation for date and time value. And with them, you can use DateFormat to do whatever formatting you want
(It will be even better to use Joda time LocalDate and LocalTime for your date and time, but that's a bit off topic)
You can't format your dates because they are String objects and SimpleDateFormat needs Date objects.
You should consider a different way of storing them (either as Date or Calendar). See below:
public class Event
{
private Date date;
private Date time;
public Event(String d, String t)
{
String[] details = d.split("\\-");
Calendar c = Calendar.getInstance();
c.set(Integer.parseInt(details[0]), Integer.parseInt(details[1]), Integer.parseInt(details[2]));
date = c.getTime();
details = t.split(":");
c.set(Integer.parseInt(details[0]), Integer.parseInt(details[1]), Integer.parseInt(details[2]));
time = c.getTime();
}
public String printDate()
{
SimpleDateFormat format = new SimpleDateFormat("dd/MM/YYYY");
String newDate = format.format(date);
return newDate;
}
// rest of you class can stay the way it is
}
You can format java.util.Date or java.sql.Date (which is subclass of java.util.Date) using date formatter, eg:
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
String dateStr = df.format(date);
Using jdbc ResultSet getDate() method you can obtain java.sql.Date object which you can print in any format using method above
Similar techniques can also be used to parse string in any format into a java.util.Date object
Date date = df.parse(dateStr);
Check the javadoc for the right formatting codes. Try this:
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
I want to set a hand written String as the date for a Date object. What I'm trying to say is that I want to do is this:
String date= [date string here!!!];
Date mydate = new Date(date);
Something like that. The reason I want to do this is because I want my network to have standard Date and Time because since I run them from the same machine the time is being taken from the same clock and it gets different time every time. So I want to get that time and also add 1-2 seconds in the end so I can test my nodes with different times.
Java is strongly typed language. You cannot assign string to Date. However you can (and should) parse string into date. For example you can use SimpleDateFormat class like the following:
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
Date date = fmt.parse("2013-05-06");
you'll want to use dateformatter
DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
Date date = formatter.parse("01/29/02");
String string = "January 2, 2010";
Date date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(string);
System.out.println(date); // Sat Jan 02 00:00:00 BOT 2010
updated
String string ="2013-04-26 08:34:55.705"
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(string);
System.out.println(date);
I'm having a weird situation with Java Calendar. I'm using dozer mapper to map the objects.
I want to write a method that will convert this object to the following format. yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
say element 2010-11-11T09:30:47.000Z
public Calender getValue(Date source,Calender c) {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.setTime(source);
return calendar;
}
When I run the program, it is printing
2010-11-11T04:00:47.000Z - Because we are setting the Timezone to be GMT, (9.30 - 5.30 = 4.00)
I want my object to have same format and value.if I don't set TimeZone to GMT, it will show as 2008-11-21T09:30:47.000+05:30.
I want it as 2010-11-11T09:30:47.000Z.
I tried added 5.30 to calender.
calendar.add(Calendar.HOUR, 5);
calendar.add(Calendar.MINUTE, 30)
then it works.But if this is ran from any other place, difference won't be 5.30.So I cannot add 5.30 to calenderget
Is there any way to get rid of this problem? I want to return Calender object.
Any suggestions or help would be much appreciated
Use a pattern. F.E:
String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
Also,
SimpleDateFormat dateformatyyyyMMdd = new SimpleDateFormat("yyyyMMdd");
String date_to_string = dateformatyyyyMMdd.format(dateNow);
you can use SimpleDateFormat like this.
SimpleDateFormat formatter, FORMATTER;
formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
String oldDate = "2011-03-10T11:54:30.207Z";
Date date = formatter.parse(oldDate.substring(0, 24));
FORMATTER = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSS");
System.out.println("OldDate-->"+oldDate);
System.out.println("NewDate-->"+FORMATTER.format(date));
Output OldDate-->2011-03-10T11:54:30.207Z NewDate-->10-Mar-2011 11:54:30.207
I have a web-form where users can select a date from a calendar pop-up and a time from a dropdown. At the moment I am trying to store the date using a Date object.
#Required
public Date date;
And the output of this object is something like:
January 1, 1970, 00:00:00 GMT
What I would really like to do is separate this and store the date in a format like 27/02/2013 and have the time as a separate object in 24 hour format e.g. 23:45. I am unsure how to do this with java.
Resolved using SimpleDateFormat:
//also the import
import java.text.*;
#Required
public SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yy");
public String date = simpleDateFormat.format(new Date());
#Required
public SimpleDateFormat simpleTimeFormat = new SimpleDateFormat("hh:mm");
public String time = simpleTimeFormat.format(new Date());
Have a look at the Calendar class. It has all of the methods required to get different "date parts". Also, look at the SimpleDateFormat class in java to format the date in needed way.
Calendar - http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
SimpleDateFormat - http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Try this:
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm:ss");
String dateAsString = dateFormatter.format(date);
String timeAsString = timeFormatter.format(date);