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();
}
Related
This is my date string,
String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";
I want to get only date with "yyyy-MM-dd" format.
How can I get date with this format.
I tried with below code, But it's not working.
String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";
Date date = null;
try {
date = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss Z").parse(dd);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String newstr = new SimpleDateFormat("yyyy-MM-dd").format(date);
System.out.println("\n"+newstr+"\n");
java.time
Using java.time, the modern Java date and time API, it’s pretty simple when you know how:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"MMM-dd-uuuu HH:mm:ss.SSS zzz(xx)", Locale.ROOT);
String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";
LocalDate date = LocalDate.parse(dd, formatter);
System.out.println(date);
Output is:
2019-11-08
As we can see, your string is in UTC. I have assumed that you also want your date in UTC. If not, we need to parse into a ZonedDateTime, convert to your desired zone and then format into your desired output format with only the date.
I assumed that UTC(+0000) is a time zone abbreviation followed by an offset in brackets.
The date and time classes that you were trying to use, Date and SimpleDateFormat, are poorly designed and long outdated, the latter in particular notoriously troublesome. You should not use them. java.time is so much nocer to work with.
What went wrong in your code?
MM is for two-digit month number, for example 11 for November or 08 for August. For month abbreviation like Nov you need MMM.
Z is for offset like +0000. While this does appear in your string, the text UTC comes before it. Edit: Confusingly SimpleDateFormat parses UTC as time zone and then ignores the remainder of the string. I for my part shouldn’t want to rely on this behaviour.
I recommend you specify a locale with your formatter to control which language it expects in the string. With MMM for month abbreviation, if you rely on the default locale and it happens to be Welsh, your formatter will expect Tach for November.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Related question: Why can't this SimpleDateFormat parse this date string?
Don't use SimpleDateFormat and Date classes those are legacy, Use java-8 modern date time API classes, First create DateTimeFormatter with the input format date
String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM-dd-uuuu HH:mm:ss.SSS zzz(Z)");
And then use OffsetDateTime to parse it with particular offset (In your case UTC)
OffsetDateTime dateTime = OffsetDateTime.parse(dd,formatter);
And then get the LocalDate
System.out.println(dateTime.toLocalDate());
The pattern you provide in the SimpleDateFormatter class constructor should match with the provided output.
e.g. since you are providing month as three letters, use 3 M's in the pattern.
String dd = "Nov-08-2019 07:00:28";
Date date = null;
try {
date = new SimpleDateFormat("MMM-dd-yyyy HH:mm:ss").parse(dd);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String newstr = new SimpleDateFormat("yyyy-MM-dd").format(date);
System.out.println("\n"+newstr+"\n");
}
String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";
Date date = null;
try {
date = new SimpleDateFormat("MMM-dd-yyyy hh:mm:ss.S z").parse(dd);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String newstr = new SimpleDateFormat("yyyy-MM-dd").format(date);
System.out.println("\n"+newstr+"\n");
This might help you.
Your input string is
String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";
You want is
yyyy-MM-dd
first it is not possible you should be getting yyyy-MMM-dd for that the code will be
String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";
Date date = null;
try {
date = new SimpleDateFormat("MMM-dd-yyyy HH:mm:ss Z").parse(dd);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String newstr = new SimpleDateFormat("yyyy-MM-dd").format(date);
System.out.println("\n"+newstr+"\n");
"Nov" is known as "MMM" format so it will throw the exception.
Try this code: https://onlinegdb.com/H1keFI5oS
import java.util.Date;
import java.text.SimpleDateFormat;
public class Main
{
public static void main(String []args){
String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";
Date date = null;
try {
date = new SimpleDateFormat("MMM-dd-yyyy HH:mm:ss").parse(dd);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String newstr = new SimpleDateFormat("yyyy-MM-dd").format(date);
System.out.println("\n"+newstr+"\n");
}
}
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);
I am trying to take date in string and its input format string and converting the date in output format. However after conversion into Date, the java code increases the number of hours by one. I am not able to understand what causes the bug.
My Code:
try {
DateFormat outputFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
DateFormat inputFormat = new SimpleDateFormat(format);
Date date = inputFormat.parse(parameterValue);
parameterValue = outputFormat.format(date);
return parameterValue;
} catch (ParseException ex) {
// take action
}
format string: ddMMMyyyy / hh:mm z
Input Date: 07DEC2015 / 10:02 GMT
Output Date: 07/12/2015 11:02:00
outputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
resolved it.
If you don't want to use timezone, in java 8 you can use LocalDate/LocalTime/LocalDateTime:
LocalDateTime localDateTimeInstance = LocalDateTime.parse(dateToBeConverted, DateTimeFormatter.ofPattern(formatOfDateToBeConverted));
return localDateTimeInstance.format("dd/MM/yyyy hh:mm:ss");
/*
Also check out ZoneDate, ZoneTime (for timezone)
Checkout - LocalDate, LocalTime
*/
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.
I'm trying to convert string to date format.I trying lot of ways to do that.But not successful. my string is "Jan 17, 2012". I want to convert this as " 2011-10-17".
Could someone please tell me the way to do this? If you have any worked through examples, that would be a real help!
try {
String strDate = "Jan 17, 2012";
//current date format
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy");
Date objDate = dateFormat.parse(strDate);
//Expected date format
SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
String finalDate = dateFormat2.format(objDate);
Log.d("Date Format:", "Final Date:"+finalDate)
} catch (Exception e) {
e.printStackTrace();
}
String format = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
Which produces this output when run in the PDT time zone:
yyyy-MM-dd 1969-12-31
yyyy-MM-dd 1970-01-01
For more info look at here
I suggest using Joda Time, it's the best and simplest library for date / dateTime manipulations in Java, and it's ThreadSafe (as opposed to the default formatting classes in Java).
You use it this way:
// Define formatters:
DateTimeFormatter inputFormat = DateTimeFormat.forPattern("MMM dd, yyyy");
DateTimeFormatter outputFormat = DateTimeFormat.forPattern("yyyy-MM-dd");
// Do your conversion:
String inputDate = "Jan 17, 2012";
DateTime date = inputFormat.parseDateTime(inputDate);
String outputDate = outputFormat.print(date);
// or:
String outputDate = date.toString(outputFormat);
// or:
String outputDate = date.toString("yyyy-MM-dd");
// Result: 2012-01-17
It also provides plenty of useful methods for operations on dates (add day, time difference, etc.). And it provides interfaces to most of the classes for easy testability and dependency injection.
Why do you want to convert string to string try to convert current time in milisecond to formated String,
this method will convert your milisconds to a data formate.
public static String getTime(long milliseconds)
{
return DateFormat.format("MMM dd, yyyy", milliseconds).toString();
}
you can also try DATE FORMATE class for better understanding.
You can't convert date from one format to other. while you are taking the date take you have take the date which ever format the you want. If you want the date in yyyy-mm-dd. You can get this by using following way.
java.util.Calendar calc = java.util.Calendar.getInstance();
int day = calc.get(java.util.Calendar.DATE);
int month = calc.get(java.util.Calendar.MONTH)+1;
int year = calc.get(java.util.Calendar.YEAR);
String currentdate = year +"/"+month +"/"+day ;
public static Date getDateFromString(String date) {
Date dt = null;
if (date != null) {
for (String sdf : supportedDateFormats) {
try {
dt = new Date(new SimpleDateFormat(sdf).parse(date).getTime());
break;
} catch (ParseException pe) {
pe.printStackTrace();
}
}
}
return dt;
}
Try this simple method:
fun getFormattedDate(strDate:String): String {
try {
val dateFormat = SimpleDateFormat("dd/mm/yyyy")//old format
val dateFormat2 = SimpleDateFormat("mm/dd/yyyy")//require new formate
val objDate = dateFormat.parse(strDate)
return dateFormat2.format(objDate)
} catch (e:Exception) {
return ""
}
}