How parser String to date formate - java

I have doubts about how parser a String (EditText) for an Date formate
I need sent to my DataBase in this format: 1991-04-01 00:00:00
But my code return in this format: Thur Apr 11 00:00:00 BTR 1991
Can you please help me in parse my EditText to Date in this format: 1991-04-01 00:00:00
follow my code:
Date newFormat = new Date();
String birthday = (mEditTextBirthday.getText().toString());
try {
newFormat = format.parse(birthday+" 00:00:00");
} catch (ParseException e) {
e.printStackTrace();
}
mUser.setBirthday(newFormat);

Simply use
SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
String birthday = mEditTextBirthday.getText();
Date = new Date();
try {
date = format.parse(birthday + " 00:00:00");
} [...]
Looks like you are using a wrongly defined SimpleDateFormat.
JavaDoc provides you with many examples.

Related

How can i get simple yyyy-MM-dd out of this date?

I have String with date format dd.MM.yyyy, and I want to upload it to my MS SQL server, but the required format is yyyy-MM-dd. I tried this but it doesn't work like I want to.
String expDate = mDatePickerBtn.getText().toString();
Date date = null;
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
try {
date = format.parse(expDate);
expDate = date.getYear() + "-" + date.getMonth() + "-" + date.getDay();
} catch (ParseException e) {
e.printStackTrace();
}
For example if I pass 31.12.2032 to the expDate, the date variable will cointain "Fri Dec 31 00:00:00: GMT+01:00 2032", and the expDate will contain "132-11-5" and I don't even know why.
I would use DateTimeFormatter but my minimal API level is 24.
My question is: where did I make mistake or how else can I get correct format out of this?
Go compile your app with Android Gradle Plugin 4.0.0+ and use java.time then like this:
public static void main(String[] args) {
// get / provide the String to be parsed
String expDate = "31.12.2032";
// provide a pattern that parses such a date
String pattern = "dd.MM.uuuu";
// create a DateTimeFormatter with this pattern
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
// parse the String with the DateTimeFormatter
LocalDate expLocalDate = LocalDate.parse(expDate, dtf);
// print the default format of a LocalDate
System.out.println(expLocalDate);
// print the LocalDate using the pattern created for parsing
System.out.println(expLocalDate.format(dtf));
// create a totally different DateTimeFormatter inline and format the date differently
System.out.println(expLocalDate.format(DateTimeFormatter.ofPattern("EEE, dd 'of' MMMM uuuu",
Locale.ENGLISH)));
}
The output would be this:
2032-12-31
31.12.2032
Fri, 31 of December 2032
Try this way
String expDate = mDatePickerBtn.getText().toString();
Date date = null;
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
try {
date = format.parse(expDate);
SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
format1.format(date);
expDate = format1.format(date);
} catch (ParseException e) {
e.printStackTrace();
}

How change String to Date format

I have this string: 2018-09-22 10:17:24.772000
I want to convert it to Date:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
String sdate = "2018-09-22 10:17:24.772000";
Date dateFrom = simpleDateFormat.parse(sdate);
but it shows: Sat Sep 22 10:17:24 GMT+03:30 2018
Here is what you should do instead, you are printing date object itself, you should print its format.
I will provide the code with old date api and new local date api :
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
String sdate = "2018-09-22 10:17:24.772000";
Date dateFrom = simpleDateFormat.parse(sdate);
System.out.println(dateFrom); // this is what you do
System.out.println(simpleDateFormat.format(dateFrom)); // this is what you should do
// below is from new java.time package
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
System.out.println(LocalDateTime.parse(sdate, formatter).format(formatter));
output is :
Sat Sep 22 10:30:16 EET 2018
2018-09-22 10:30:16.000000
2018-09-22 10:17:24.772000
Hope This will help you
public class Utils {
public static void main(String[] args) {
String mytime="2018-09-22 10:17:24.772000";
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss.SSSSSS");
Date myDate = null;
try {
myDate = dateFormat.parse(mytime);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd");
String finalDate = timeFormat.format(myDate);
System.out.println(finalDate);
}
}
Looks to me like you have converted it to a Date. What is your desired result? I suspect what you are wanting to do is to create another Simple date format that shows your expected format and then use simpledateformat2.format(dateFrom)
I should also point out based on past experience that you should add a Locale to your simple date formats otherwise a device with a different language setting may not be able to execute this code
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS", Locale.US);

How to get a required Date format using SimpleDateFormat [duplicate]

This question already has answers here:
Java date format conversion - getting wrong month
(8 answers)
How to parse a date? [duplicate]
(5 answers)
Closed 5 years ago.
My method is :
public String changeCurrentDate(Integer variant){
String currentTime = TestApp.getInstance().getDriver().findElement(By.id("common.HeaderComponent.mainLayout.serverTimeLabel")).getText();
String currentDate = currentTime.substring(0, 10);
System.out.println("currentDate " +currentDate);
String date = null;
DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
try{
Date date3 = df.parse(currentDate);
df.format(date3);
System.out.println("date3 " +date3);
Date previousDate = DateUtils.addDays(date3, variant);
date = previousDate.toString();
return date;
}catch (Exception e){
}
return date;
}
Note : currentTime variable always have the value like "18/12/2017"
I'm expecting result of date in dd/mm/yyyy format. but it always gives "Wed Jan 18 00:12:00 IST 2017" like this.
Run Time Results :
currentDate 18/12/2017
date3 Wed Jan 18 00:12:00 IST 2017
You should return the formatted date, not the toString() of the date. Try this:
Date previousDate = DateUtils.addDays(date3, variant);
return df.format(previousDate);
df.format simply returns a String representation of the Date with the format applied, therefore the line you have in your code has no effect.
Try changing you code to use the formatted output instead:
public String changeCurrentDate(Integer variant){
String currentTime = TestApp.getInstance().getDriver().findElement(By.id("common.HeaderComponent.mainLayout.serverTimeLabel")).getText();
String currentDate = currentTime.substring(0, 10);
System.out.println("currentDate " +currentDate);
DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
Date date3 = df.parse(currentDate);
System.out.println("date3 " + df.format(date3));
Date previousDate = DateUtils.addDays(date3, variant);
return previousDate.toString();
}
Also - its bad to catch Exception, so you should remove that.
We should always try to find out the more convenient way so that we can further use it. In that case, we can use below method:
public String dateString(String input) {
SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
String formattedDate = "";
try {
Date date = parser.parse(input);
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
formattedDate = formatter.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return formattedDate;
}
You can easily call this method like below:
Date date3 = df.parse(currentDate);
df.format(date3);
String input = date3.toString();
String requiredDate = dateString(input);
System.out.println("requiredDate: "+ requiredDate);
Which returns the output like below:
requiredDate: 18/12/2017

Parse yyyy-MM-DD String to Date yyyy-MM-DD in Android?

String startDateStr = "2017-02-03"
DateFormat formatter = new SimpleDateFormat("yyyy-MM-DD",Locale.US);
Date date = (Date)formatter.parse(startDateStr);
2017-02-03 date is parsed to Tue Jan 03 00:00:00 GMT+05:45 2017
Did I
miss something?
Update
I needed a string to be converted to a date object
while maintaining the same format.
The reason for this is I want to make use of public boolean after(Date when) method
This will work ^_^
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
DateFormat outputFormat = new SimpleDateFormat("dd MMM yyyy");
String startDateStr ="2017-02-03";
Date date = null;
try {
date = inputFormat.parse(startDateStr);
String startDateStrNewFormat = outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
Little explanation of your output :
D is Day in year (1-365)
d is day in month (1-31)
Check the document
Use SimpleDateFormat type for fomatter. You are creating DateFormat object but using SimpleDateFormat.
String startDateStr = "2017-02-03"
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",Locale.US);
Date date = (Date)formatter.parse(startDateStr);
Yes you missed something. You used DD instead of dd in your yyyy-MM-DD format string. Here is how you do it:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(new Date());

Converting a String of a Date to a Date

I'm having troubles converting a String of a date entered by a user to an actual Date that can be sent to a database.
Ultimately the user enters a date in the format of YYYY-MM-DD and it gets sent to the database. I'm trying this:
String date = "2015-03-02";
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
Date parsedDate = sdf.parse(date);
this is all it outputs
Sun Dec 28 00:00:00 CST 2014
Your format String should be yyyy-MM-dd; and something like
String date = "2015-03-02";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date parsedDate = sdf.parse(date);
System.out.println(sdf.format(parsedDate));
} catch (ParseException e) {
e.printStackTrace();
}
Output is
2015-03-02

Categories