Convert TextField into Date - java

I want to convert TextField into Date.
private TextField fieldDateAgentAdded;
Date dateAgentAdded;
dateAgentAdded(fieldDateAgentAdded.toString())
Can you tell me how I can convert the String into Date?

Well you can do it in one line using:
dateAgentAdded(new SimpleDateFormat("dd/MM/yy", Locale.ENGLISH).parse(fieldDateAgentAdded.toString());
There is thousand of way to write date, so you don't really have choice, you need to specify the expected format.

You can use SimpleDateFormat class.
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = DATE_FORMAT.parse("2013-12-4");
System.out.println(date.toString());

The easiest way is to use the SimpleDateFormat class. Here is an example:
String dateString = "May 14, 2012";
Date date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(dateString);
Now date should contain the date May 14, 2012. For more information see the javadoc.

Related

Date value converted from Calendar outputs different format than String [duplicate]

I have the following scenario :
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(dateFormat.parse("31/05/2011"));
gives an output
Tue May 31 00:00:00 SGT 2011
but I want the output to be
31/05/2011
I need to use parse here because the dates need to be sorted as Dates and not as String.
Any ideas ??
How about:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(dateFormat.format(dateFormat.parse("31/05/2011")));
> 31/05/2011
You need to go through SimpleDateFormat.format in order to format the date as a string.
Here's an example that goes from String -> Date -> String.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = dateFormat.parse("31/05/2011");
System.out.println(dateFormat.format(date)); // prints 31/05/2011
// ^^^^^^
Use the SimpleDateFormat.format
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
String sDate= sdf.format(date);
You can use simple date format in Java using the code below
SimpleDateFormat simpledatafo = new SimpleDateFormat("dd/MM/yyyy");
Date newDate = new Date();
String expectedDate= simpledatafo.format(newDate);
It makes no sense, but:
System.out.println(dateFormat.format(dateFormat.parse("31/05/2011")))
SimpleDateFormat.parse() = // parse Date from String
SimpleDateFormat.format() = // format Date into String
If you want to simply output a date, just use the following:
System.out.printf("Date: %1$te/%1$tm/%1$tY at %1$tH:%1$tM:%1$tS%n", new Date());
As seen here. Or if you want to get the value into a String (for SQL building, for example) you can use:
String formattedDate = String.format("%1$te/%1$tm/%1$tY", new Date());
You can also customize your output by following the Java API on Date/Time conversions.
java.time
Here’s the modern answer.
DateTimeFormatter sourceFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu");
DateTimeFormatter displayFormatter = DateTimeFormatter
.ofLocalizedDate(FormatStyle.SHORT)
.withLocale(Locale.forLanguageTag("zh-SG"));
String dateString = "31/05/2011";
LocalDate date = LocalDate.parse(dateString, sourceFormatter);
System.out.println(date.format(displayFormatter));
Output from this snippet is:
31/05/11
See if you can live with the 2-digit year. Or use FormatStyle.MEDIUM to obtain 2011年5月31日. I recommend you use Java’s built-in date and time formats when you can. It’s easier and lends itself very well to internationalization.
If you need the exact format you gave, just use the source formatter as display formatter too:
System.out.println(date.format(sourceFormatter));
31/05/2011
I recommend you don’t use SimpleDateFormat. It’s notoriously troublesome and long outdated. Instead I use java.time, the modern Java date and time API.
To obtain a specific format you need to format the parsed date back into a string. Netiher an old-fashioned Date nor a modern LocalDatecan have a format in it.
Link: Oracle tutorial: Date Time explaining how to use java.time.
You already has this (that's what you entered) parse will parse a date into a giving format and print the full date object (toString).
This will help you.
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
print (df.format(new Date());
I had something like this, my suggestion would be to use java for things like this, don't put in boilerplate code
This looks more compact. Finishes in a single line.
import org.apache.commons.lang3.time.DateFormatUtils;
System.out.println(DateFormatUtils.format(newDate, "yyyy-MM-dd HH:mm:ss"));

How to get the correct format of dateand time in mysql?

When I get the date and time in MySQL it retrieves it in this format:
2016-01-14 14:24:00.0
Where does the .0 come from and how do I get this format with Java:
2016-01-14 14:24:00
You can use 2 ways to do this.
Split the date string at '.'
String date = "2016-01-14 14:24:00.0";
String newDate = date.split("\\.")[0];
System.out.println(newDate);
Use SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = sdf.parse("2016-01-14 14:24:00.0");
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));
If you are getting date as string, you can use Format to get the format you need:
SELECT DATE_FORMAT(YourDateField, '%d-%m-%Y %T')
FROM YourTable
Have a look here for all possible formats
If you are getting your date as a java.sql.Timestamp, you can get the corresponding java.util.Date instance very easily and then format it to the desired string representation with a java.text.SimpleDateFormat class.

Convert long timestamp to Java.util.Date in french

I want to know how I can convert a timestamp in the date format.
For example : 1415337782000 should be converted to "7 nov. 2014".
This is what I've tried so far :
Date date=new Date(location.date);
SimpleDateFormat df = (SimpleDateFormat) SimpleDateFormat.getDateInstance(SimpleDateFormat.MEDIUM);
String dateText = df.format(date);
The timestamp is stored in location.date in long type.
Thanks in advance for your help.
You can pass a format to the SimpleDateFormat constructor, like this
SimpleDateFormat format= new SimpleDateFormat("d MMM, yyyy");
String dateText = format.format(new Date());
You can find the constants in this link:
http://developer.android.com/reference/java/text/SimpleDateFormat.html
For better internationalization, you can define a string resource containing the desired format based on the location of the user. Something like
SimpleDateFormat format = new SimpleDateFormat(getResources().getString(R.string.YOUR_FORMAT));

Set a String variable to a Date object in java

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

Date converter in JAVA [duplicate]

This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Closed 9 years ago.
I have date like Tue Mar 19 00:41:00 GMT 2013, how to convert it to 2013-03-19 06:13:00?
final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
final Date date = bdate;
Date ndate = formatter.parse(formatter.format(date));
System.out.println(ndate);
gives the same date.
Use two SimpleDateFormat objects with appropriate formats and use the first to parse the string into a date and the second to format the date into a string again.
As the first answer says. First parse your date with SimpleDateFormat like this:
Date from = new SimpleDateFormat("E M d hh:mm:ss z yyyy").parse("Tue Mar 19 00:41:00 GMT 2013");
Then use that to format the resulting date object with another instance of SimpleDateFormat like this:
String to = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(from);
See the javadoc of SimpleDateFormat here. Hope that helps.
One major thing that the others have left out is dealing with the timezone (TZ). Anytime you use a SimpleDateFormat to go to/from a string representation of the date, you really need to be aware of what TZ you're dealing with. Unless you explicitly set the TZ on the SimpleDateFormat, it will use the default TZ when formatting/parsing. Unless you only deal with date strings in the default timezone, you'll run into problems.
Your input date is representing a date in GMT. Assuming that you also want the output to be formatted as GMT, you need to make sure to set the TZ on the SimpleDateFormat:
public static void main(String[] args) throws Exception
{
String inputDate = "Tue Mar 19 00:41:00 GMT 2013";
// Initialize with format of input
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
// Configure the TZ on the date formatter. Not sure why it doesn't get set
// automatically when parsing the date since the input includes the TZ name,
// but it doesn't. One of many reasons to use Joda instead
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = sdf.parse(inputDate);
// re-initialize the pattern with format of desired output. Alternatively,
// you could use a new SimpleDateFormat instance as long as you set the TZ
// correctly
sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));
}
Use SimpleDateFormat in this way:
final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
final Date date = new Date();
System.out.println(formatter.format(date));
If you do any calculations or parsing with dates please use JodaTime because the standard JAVA date support is really buggy

Categories

Resources