This question already has answers here:
Convert a string to a GregorianCalendar
(2 answers)
Closed 6 years ago.
This is the code to get date format
GregorianCalendar calDate = new GregorianCalendar();
The constructor will hold value of format as 2015,7,15
what I want to achieve is to be able to set this format from a text field. For example
GregorianCalendar calDate = new GregorianCalendar(Formats.getText());
But I have errors and is not working.
Please whats is right code?
Resolved with the below code
String nnhh= ""+Firstname.getText();
String someDate = ""+nnhh;
SimpleDateFormat sdf = new SimpleDateFormat("MM,dd,yyyy");
try {
Date date = sdf.parse(someDate);
long dd=date.getTime();
Firstname.setText(""+dd);} catch (ParseException e) {
e.printStackTrace();
}
and dd was in TimeInMillis just as I wanted. Thanks
To be able to parse it, I would suggest you to use something like:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("y M d VV m s H");
The formatter requires all of these elements. (see https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html for formatting details).
And this should be all you need:
GregorianCalendar calDate = GregorianCalendar.from(ZonedDateTime.parse(dateToParse, formatter));
Related
This question already has answers here:
Datetime parsing error
(2 answers)
Closed 5 years ago.
I am struggling with a date string, I need to parse into the java ‘Date’ object.
Here is what I have got so far:
try {
String value = "2017-11-23T14:00:49.184000000Z";
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'";
SimpleDateFormat parser = new SimpleDateFormat(pattern);
Date date = parser.parse(value);
} catch (ParseException e) {e.printStackTrace();}
It currently throws a ParseException “Unparseable date” and I can’t get it to work.
Any help is highly appreciated!
Thanks
Use Instant from java.time package (java 8) instead, it should look like below
String value = "2017-11-23T14:00:49.184000000Z";
Instant instant = Instant.parse(value);
Date date = Date.from(instant);
System.out.println(date);
you can use timeZone as well like this as another solution.
TimeZone tz = TimeZone.getTimeZone("Asia/Calcutta");
Calendar cal = Calendar.getInstance(tz);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'");
sdf.setCalendar(cal);
cal.setTime(sdf.parse("2017-11-23T14:58:00.184000000Z"));
Date date = cal.getTime();
System.out.println(date);
This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 5 years ago.
I'm trying to convert a String month to a int month in Java. I do not want my program to have a lot of logic on it, so I'm trying to do it without having to create a switch case or a Enum. If not possible, i'll have to do deal with it and create that logic...
My String Sample is this:
String date = "2017-Oct-27";
And I want it to be like this:
String date = "2017-10-27";
Can anyone help me please?
Thanks
Please try below code
String input = "2017-Oct-27";
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MMM-dd");
String formattedDate= "";
Date date;
try {
date = parser.parse(input);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
formattedDate = formatter.format(date);
} catch (ParseException e1) {
e1.printStackTrace();
}
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
I'm trying to convert a date in dd-MM-yyyy format from YYYY-MM-dd hh:mm:ss.ms
in java using the below code but m unable to get the desired value
String dob = (new SimpleDateFormat("dd-MM-yyyy")).format(customerEntity.getDob().toString());
customerEntity.getDob().toString is providing me this value 1987-06-12 00:00:00.0
But when i'm parsing it to the String dob it produces 163-06-1987 as the output whereas i want the output like 12-06-1987 .
Any help will be appreciable, thanks well in advance
format method in SimpleDateFormat take a Date as argument and not a String
public static void main(String[] args) {
String dateStr = "29/12/2016";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {
Date date = sdf.parse(dateStr);
sdf = new SimpleDateFormat("dd-MM-yyyy");
System.out.println(sdf.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
Try parsing your string date into a Date first in format it is coming. Post that pass on that Date object to a format in the format you want your output.
As in below :
Date dob = (new SimpleDateFormat("yyyy-MM-dd")).parse("1987-06-12 00:00:00.0");
String dob1 = (new SimpleDateFormat("dd-MM-yyyy")).format(dob);
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
how to convert this string "2016-10-08T01:00:00-07:00" to date object in Java?
I want to know what is string format to use with SimpleDateFormat.
I have try
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
try {
Date date = format.parse("2016-10-08T01:00:00-07:00");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Change your format from Z to X will work . Detail is here
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
Date date = format.parse("2016-10-08T01:00:00-07:00");
should be
Date date = format.parse("2016-10-08T01:00:00-0700");
The time zone should not have a colon delimiting hours and minutes.
You can try SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
Best solution to refer # Change date format in a Java string
This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
Closed 6 years ago.
How do I take date as input and add 3 in selected date then set label with updated date in java swings? My date format is YYYY-MM-DD.
eg if I enter 2016-04-04 then change date to 2016-04-07 and set a label to it.
i Tried this . . gives me wring output. 1970-03-01 is ouput
plz help
To use this code:
Declare the following variables:
String youdate = "2016-04-04";
Date mmdate;
TextView tv;
and use this code:
try {
mmdate = new SimpleDateFormat("yyyy-MM-dd").parse(youdate);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar c = Calendar.getInstance();
c.setTime(mmdate);
c.add(Calendar.DATE, 3);
Date newdate = c.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
tv = (TextView)findViewById(R.id.tv);
tv.setText(format.format(newdate));
tv contains 2016-04-07
You can add 72 hours in date format to create new date with +3 days addition.
public String nextDate(String date) {
LocalDate parsedDate = LocalDate.parse(date);
LocalDate addedDate = parsedDate.plusDays(3);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return addedDate.format(formatter);
}