Convert String month to Int month [Java] [duplicate] - java

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

Related

Convert a Date dd/MM/yyyy to dd-MM-yyyy in java [duplicate]

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

I have created a date using SimpleDateFormat, how do I convert into a string? [duplicate]

This question already has answers here:
Convert java.util.Date to String
(17 answers)
Closed 6 years ago.
newDate =(newDaysAdded+"/"+month+"/"+year);
SimpleDateFormat date = new SimpleDateFormat("dd/MM/yyyy");
try {
Date newDateOne = date.parse(newDate);
When I try:
view.writeToScreen(newDateOne);
I get the error:
Incompatible types: Date cannot be converted to String.
Here is the writeToScreen method:
void writeToScreen(String s);
However from there I can't turn into a string, I have tried:
String test = Date.toString(newDateOne);
Can anyone help?
Based on your existing code you could use:
writeToScreen (date.format(newDateOne));
Change
view.writeToScreen(newDateOne);
to
view.writeToScreen(newDateOne.toString());
Try to look at all the methods... Either in the API doc or even in the intellisense in a code editor
public static void main(String[] args) {
String newDaysAdded = "05";
String month = "04";
String year = "2014";
String newDateString = (newDaysAdded+"/"+month+"/"+year);
SimpleDateFormat SDF = new SimpleDateFormat("dd/MM/yyyy");
try {
Date newDate = SDF.parse(newDateString); //Takes string and convert it to date
//If i understand the question you dont just want to print it as a date...
System.out.println(newDate); //Sat Apr 05 00:00:00 CAT 2014
//You want to print it as a string inf the format you specified in the SDT
System.out.println(SDF.format(newDate)); //Print as in05/04/2014
} catch (ParseException e) {
e.printStackTrace();
}
}

Java convert Date in string to Date [duplicate]

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

Android - How can I convert a date/time string into date and time integers? [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
I am trying to convert back a stored date/time string in the format of (MM dd, yyyy, HH:mm [AM/PM]), I followed this post to create the string date/time. My current code properly fetches the stored date time but when I try to parse it and display the Date object "d" I get a null output.
Java code:
public void loadExamData(Cursor cursor) {
exam.setText(cursor.getString(1));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date d = sdf.parse(cursor.getString(2));
} catch (ParseException ex) {
Logger.getLogger(edit_exam_form.class.getName()).log(Level.SEVERE, null, ex);
}
location.setText(cursor.getString(3));
}
First you have to parse the date string with the current pattern and then format it with your desired pattern.
For Example, if your current date string is like this [10/10/2016 14:30].
String curDate = "10/10/2016 14:30";
SimpleDateFormat curDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
SimpleDateFormat desiredDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = curDateFormat.parse(curDate);
String desiredDate = desiredDateFormat.format(date);
// you can use this date string now
} catch (Exception e) {}

How do I set the construtor value from a textfield [duplicate]

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

Categories

Resources