How to convert String to TimeStamp [duplicate] - java

This question already has answers here:
Java: Convert String to TimeStamp
(11 answers)
Closed 5 years ago.
I want to convert a string to Timestamp. Here is the sample code.
I want to parse the string to Timestamp..
I have tried to convert it like
String date format is "18/01/11";
String date = "25/07/2017";
Timestamp ts =Timestamp.valueOf(date);
DateUtil.convertDate(ts.toString());
convertDate:-
SimpleDateFormat inputFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date d = null;
try {
d = inputFormat.parse(input);
} catch (ParseException e) {
System.out.println("Date Format Not Supported");
e.printStackTrace();
}
SimpleDateFormat outputFormat = new SimpleDateFormat("dd/MM/yyyy");
return outputFormat.format(d).toString();
if I write like that i am getting the following error
Date Format Not Supported
Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]

I think you are using just opposite formats if your input format is dd/MM/yyyy:
//date format of input
SimpleDateFormat inputFormat = new SimpleDateFormat("dd/MM/yyyy");
Date d = null;
try {
//convert string to date
d = inputFormat.parse(input);
} catch (ParseException e) {
System.out.println("Date Format Not Supported");
e.printStackTrace();
}
//output date format
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//convert date to timestamp
return outputFormat.format(d).toString();

Related

Change date format dd-MM-yyyy to yyyy-MM-dd in Java [duplicate]

This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 5 years ago.
I was trying to convert date string 08-12-2017 to 2017-12-08(LocalDate). Here is what I tried-
String startDateString = "08-12-2017";
LocalDate date = LocalDate.parse(startDateString);
System.out.println(date);
Also tried using formatter, but getting same result, an DateTimeParseException.
How can I get an output like 2017-12-08, without getting an exception?
Try this (see update below)
try {
String startDateString = "08-12-2017";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf2.format(sdf.parse(startDateString)));
} catch (ParseException e) {
e.printStackTrace();
}
Update - Java 8
String startDateString = "08-12-2017";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
System.out.println(LocalDate.parse(startDateString, formatter).format(formatter2));
First you have to parse the string representation of your date-time into a Date object.
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = (Date)formatter.parse("2011-11-29 12:34:25");
Then you format the Date object back into a String in your preferred format.
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String mydate = dateFormat.format(date);

simple date format giving wrong month [duplicate]

This question already has answers here:
SimpleDateFormat producing wrong date time when parsing "YYYY-MM-dd HH:mm"
(5 answers)
Closed 5 years ago.
I was trying to convert date from yyyy-MM-dd format to yyyy-MM format, when I run the below code with the input "2012-10-22" it is giving me an output of 2012-JAN instead of 2012-OCT. any thoughts on where I am doing wrong?
public static String dateFormatter(String presentDate)
{
String formattedDate = "";
SimpleDateFormat tempFormat = new SimpleDateFormat("YYYY-MM-DD");
SimpleDateFormat finalFormat = new SimpleDateFormat("YYYY-MMM");
try {
Date currentFormat = tempFormat.parse(presentDate);
formattedDate = finalFormat.format(currentFormat);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return formattedDate;
}
Change the first format to
SimpleDateFormat tempFormat = new SimpleDateFormat("yyyy-MM-dd");
as DD is the day in the year. 22 is definitely in January
Use this
SimpleDateFormat tempFormat = new SimpleDateFormat("yyyy-MM-dd");
Instead of
SimpleDateFormat tempFormat = new SimpleDateFormat("YYYY-MM-DD");

Android cannot compare dates with yyyy-MM-dd format [duplicate]

This question already has answers here:
Java Unparseable Date Exception
(3 answers)
Closed 7 years ago.
Android comparing date with yyyy/MM/dd format only force closes when compared with yyyy-MM-dd format.
final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",
Locale.getDefault());
String d = "2016/12/29";
expiryDate = (Date) formatter.parse(d);
I hope this one help you.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Date finalStartTime = format.parse(YOUR_OLD_DATE);
Date finalEndTime = format.parse(YOUR_NEW_DATE);
if (finalEndTime.after(finalStartTime)) {
//your code
}
} catch (ParseException e) {
}
If still not worked let me know...
Just Change "-" to "/" :
final SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd",
Locale.getDefault());
String d = "2016/12/29";
try {
expiryDate = (Date) formatter.parse(d);
} catch (ParseException e) {
//do something
}
final SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
String d = "2016/12/29";
Date expiryDate = formatter.parse(d);

I want to parse String into java date object [duplicate]

This question already has answers here:
convert string into date format
(5 answers)
Closed 9 years ago.
Hello i have date in this format 2013-10-31T19:00:00Z now i want to display this date into yyyy/mm/dd this format.i have used this code but its giving me exception kindly help me
String startDateString = "2013-10-31T19:00:00Z";
DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
Date startDate;
try {
startDate = df.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println("Date is "+newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
Try this
String DateStr="2013-10-31T19:00:00Z";
SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
Date d = new SimpleDateFormat("yyyy-MM-dd").parse(DateStr);
System.out.println(sim.format(d));
output 2013-10-31
That's because you're specifically telling Java to look for a 4-digit year followed by a 2-digit month and 2-digit date. Taken straight from the docs, what you want is
"yyyy-MM-dd'T'HH:mm:ssZ"
You can use SimpleDateFormat's parse method to parse your date, and then use the format method to output it in the format that you prefer.
Try this:
String startDateString = "2013-10-31T19:00:00Z";
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
DateFormat outputFormat = new SimpleDateFormat("yyyy/MM/dd");
Date date = inputFormat.parse(startDateString);
System.out.println("Date is " + outputFormat.format(date));
Output:
Date is 2013/10/31
You need a separate SimpleDateFormat to parse the Datestring. Try this:
String startDateString = "2013-10-31T19:00:00Z";
DateFormat originalFormat = new SimpleDateFormat("yyyy-mm-dd'T'HH:MM:SS'Z'");
DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
Date startDate;
try {
startDate = originalFormat.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println("Date is " + newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
You need two different DateFormat. one for parse date and one for formatting date.
String startDateString = "2013-10-31T19:00:00Z";
DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:SS'Z'");
Date startDate;
try {
startDate =df1.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println("Date is "+newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "07/06/2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Here is simple example
Try this:
try {
String startDateString = "2013-10-31T19:00:00Z";
DateFormat input = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
DateFormat output = new SimpleDateFormat("yyyy/MM/dd");
Date date = input.parse(startDateString);
System.out.println("Date is " + output.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Pointing an another simple mistake in your code
You coded as DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
here mm is for minutes , for month use MM

convert a date as a string to a date as a string in another format

How can i parse String "2013-03-31", so i will get - "31 March 2013 Sun".
I tried to parse as bellow:
final SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy DD");
try {
Date data = formatter.parse(eventDate);
System.out.println(data);
} catch (Exception e) {
System.out.println(e.getMessage());
}
And i get Unparseable date: "2013-03-31" (at offset 10) exception text.
Is that String represented date is correct, and if correct how i must parse it.
Input format: "yyyy-MM-dd"
Output format: "dd MMMM yyyy EEE"
final SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
final SimpleDateFormat outputFormat = new SimpleDateFormat("dd MMMM yyyy EEE");
try {
Date data = inputFormat.parse(eventDate);
System.out.println(outputFormat.format(data));
} catch (Exception e) {
System.out.println(e.getMessage());
}
see these links
string to date
How to parse a date?
How to parse date string to Date?
date to string
Convert java.util.Date to String
How to convert current date into string in java?
You don't specify a date input format:
String eventDate = "2013-03-31";
final SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
final SimpleDateFormat outputFormat = new SimpleDateFormat(
"dd MMMM yyyy EEE");
try {
Date date = inputFormat.parse(eventDate);
System.out.println(outputFormat.format(date));
} catch (Exception e) {
e.printStackTrace();
}
public static String getGmtTmFromLcl(String inputDtm, String inputFormat, String outputFormat) throws Exception {
SimpleDateFormat format = new SimpleDateFormat(inputFormat);
Date dt = format.parse(inputDtm);
format = new SimpleDateFormat(outputFormat);
return format.format(dt);
}

Categories