I am trying to convert a date in String format into UNIX timestamp, I am able to convert it but when I check the timestamp it displays incorrect date.
I am using the following code to convert a Date in String to Unix timestamp:
String selected_date = "16/11/2015 1:34 am";
datetime.setText(selected_date);
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy hh:mm a");
Date date = null;
try {
date = dateFormat.parse(selected_date);
} catch (ParseException e) {
e.printStackTrace();
}
long unixTime = (long)date.getTime()/1000;
The output UNIX timestamp is: 1460309640
But when I convert that timestamp using a web tool it returns: 4/11/2016, 1:34:00 AM
The format
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy hh:mm a");
is not compatible with the string
String selected_date = "16/11/2015 1:34 am";
16 can't be a month!
2015 is not a year in two digits format
The right format seems to be (not sure if kk or KK depending on hours if 0 based or not)
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy kk:mm a");
Related
I am facing problems some while formatting the date:
Date : 11/06/2020 04:14:20
Date Format:dd/MM/yyyy hh:mm:ss a
Exception:
java.text.ParseException: Unparseable date: "11/06/2020 04:14:20"
Following is the code
Blockquote
public String getFormatDate(String inputDate) {
String strDate = "";
try {
DateFormat outputFormat = new SimpleDateFormat("MMMM dd, yyyy hh:mm:ss a");
DateFormat inputFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
Date date1 = inputFormat.parse(inputDate);
strDate = outputFormat.format(date1);
}catch( Exception exe) {
exe.printStackTrace();
logger.error( "[ERROR] getFormatDate:. ", exe );
}
return strDate;
}
Blockquote
Any help would be greatly appeciated.
You can check this code you have to pass the am/pm part too with the date string value as your format is expecting that.
//String date = "11/06/2020 04:14:20";
String date = "11/06/2020 04:14:20 am";
DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
https://ideone.com/3nibwJ
Use proper date-time objects for your dates and times
For the vast majority of purposes you should not keep your date and time in a string and should not convert your date and time from a string in one format to a string in another format. Keep your date and time in a ZonedDateTime or LocalDateTime object.
When you are required to accept string input, parse that input into a date-time object immediately. I am using and recommending java.time, the modern Java date and time API:
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu HH:mm:ss");
String input = "11/06/2020 04:14:20";
LocalDateTime dateTime = LocalDateTime.parse(input, inputFormatter);
System.out.println(dateTime);
Output so far is:
2020-06-11T04:14:20
Since there is no AM or PM in your string, I have assumed that 04:14:20 was the time of day from 00:00:00 through 23:59:59. If you intended otherwise, you need to explain how.
Only when you need to give string output, format your date and time back into a string of appropriate format:
DateTimeFormatter outputFormatter = DateTimeFormatter
.ofPattern("MMMM dd, yyyy hh:mm:ss a", Locale.ENGLISH);
String output = dateTime.format(outputFormatter);
System.out.println(output);
June 11, 2020 04:14:20 AM
Do provide a locale for the formatter so Java knows which language to use for the month name and the AM/PM indicator.
What went wrong in your code?
Your string has no AM nor PM: 11/06/2020 04:14:20. Yet your format pattern string requires an AM/PM marker in the end. This is what format pattern letter a signifies. So your string wasn’t in the format that you required. This was the reason for the exception that you observed.
Link
Oracle tutorial: Date Time explaining how to use java.time.
Thanks All for your help:
I have changed the source date "11/06/2020 04:14:20" to "06/11/2020 04:14:20 PM", and then after perform follwoing steps, its working for me:
Blockquote
DateFormat inputFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
inputFormat.setTimeZone( TimeZone.getTimeZone("UTC") );
Date dDate = inputFormat.parse( srcDate );
String strDeDate = formatDateToString( dDate, "dd/MM/yyyy hh:mm:ss a", "IST" );
public String formatDateToString(Date date, String format,String timeZone) {
if (date == null) return null;
SimpleDateFormat sdf = new SimpleDateFormat(format);
if (timeZone == null || "".equalsIgnoreCase(timeZone.trim())) {
timeZone = Calendar.getInstance().getTimeZone().getID();
}
sdf.setTimeZone(TimeZone.getTimeZone(timeZone));
return sdf.format(date);
}
Blockquote
below is the code I have used to add the number of days to the existing date..which gave me string output and I want that to be converted to Date format again...I have tried formating but it gave the out put -->
Date date = sdf.parse(dt);
sysout (date ) --giving me -- Mon May 05 00:00:00 PDT 2008
but I want it as YYYY-MM/DD
sdf.format(date) --Gives me 2008-05-05 which I am looking but it is a string object...but I want this to be converted to DATE type
String dt = "2008-01-01"; // Start date
System.out.println("start date "+dt);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 125); // number of days to add
dt = sdf.format(c.getTime());
System.out.println("c.getTime() "+c.getTime());
System.out.println("end date "+dt);
Date date = sdf.parse(dt);
System.out.println("last but one date in DATE form -->" +date);
System.out.println("last formatted date in string form "+sdf.format(date));
You created the format right.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
but you are using it incorrectly. you should use
sdf.format(your_unformated_date);
Here is a sample code that will convert date from String to Date type using SimpleDateFormat Class:
public static void convert()
{
String str="10:25:35";
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm:ss");
System.out.println(sdf.format(str));
}
i am getting this error while i am trying to convert a string into date.
unparasable data
Below is my code:-
String str = "hello"
Second is missing at your parse String str. So, to parse it you should not include second format at SimpleDateFormat pattern. Also correct the day and Month format. Look at the declaration of df
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");//Remove :ss
To know details of pattern, go through this docs.
Edit
String date2 = sdformatter.format(date1);// format method return String.
//Should declare with String
Full Code
String str = "25-Nov-2013 06:00 AM";
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");//Remove :ss
SimpleDateFormat sdformatter = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss a");
Date date1=null;
try {
date1 = df.parse(str);
} catch (ParseException ex) {
ex.printStackTrace();
}
String date2 = sdformatter.format(date1);
System.out.println(date2);
According to str format you should write your SimpleDateFormat,
(25-Nov-2013 06:00 AM ---> dd-MMM-yyyy hh:mm a) and for
(25-Nov-2013 06:00:30 AM-----> dd-MMM-yyyy hh:mm:ss a)
will work
Try this
long newerdate = new Date().parse("25-Nov-2013 06:30 AM");
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("dd-MM-yyyy hh:mm a");
String data = df.format(newerdate);
System.out.println(data);
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 9 years ago.
I have a string
String startDate = "06/27/2007";
now i have to get Date object. My DateObject should be the same value as of startDate.
I am doing like this
DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
Date startDate = df.parse(startDate);
But the output is in format
Jan 27 00:06:00 PST 2007.
You basically effectively converted your date in a string format to a date object. If you print it out at that point, you will get the standard date formatting output. In order to format it after that, you then need to convert it back to a date object with a specified format (already specified previously)
String startDateString = "06/27/2007";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date startDate;
try {
startDate = df.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println(newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
"mm" means the "minutes" fragment of a date. For the "months" part, use "MM".
So, try to change the code to:
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date startDate = df.parse(startDateString);
Edit:
A DateFormat object contains a date formatting definition, not a Date object, which contains only the date without concerning about formatting.
When talking about formatting, we are talking about create a String representation of a Date in a specific format. See this example:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
public static void main(String[] args) throws Exception {
String startDateString = "06/27/2007";
// This object can interpret strings representing dates in the format MM/dd/yyyy
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
// Convert from String to Date
Date startDate = df.parse(startDateString);
// Print the date, with the default formatting.
// Here, the important thing to note is that the parts of the date
// were correctly interpreted, such as day, month, year etc.
System.out.println("Date, with the default formatting: " + startDate);
// Once converted to a Date object, you can convert
// back to a String using any desired format.
String startDateString1 = df.format(startDate);
System.out.println("Date in format MM/dd/yyyy: " + startDateString1);
// Converting to String again, using an alternative format
DateFormat df2 = new SimpleDateFormat("dd/MM/yyyy");
String startDateString2 = df2.format(startDate);
System.out.println("Date in format dd/MM/yyyy: " + startDateString2);
}
}
Output:
Date, with the default formatting: Wed Jun 27 00:00:00 BRT 2007
Date in format MM/dd/yyyy: 06/27/2007
Date in format dd/MM/yyyy: 27/06/2007
try
{
String datestr="06/27/2007";
DateFormat formatter;
Date date;
formatter = new SimpleDateFormat("MM/dd/yyyy");
date = (Date)formatter.parse(datestr);
}
catch (Exception e)
{}
month is MM, minutes is mm..
The concise version:
String dateStr = "06/27/2007";
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date startDate = (Date)formatter.parse(dateStr);
Add a try/catch block for a ParseException to ensure the format is a valid date.
var startDate = "06/27/2007";
startDate = new Date(startDate);
console.log(startDate);
I am developing an application and I am stuck in converting string like 01/01/2037 01:00:00 AM
to Date
I used
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh.mm.ss.S aa")
Date d = dateFormat.parse(dateString);
but I get an error, any help will be appreciated.
you are converting this 01/01/2037 01:00:00 AM
therefore use
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa")
(more info in [documentation])1
then
Date date = dateFormat.parse("01/01/2037 01:00:00 AM");
keep in mind you have to wrap a try-catch around the parse method.
The problem is that the format you declared is nothing like the String you are trying to parse:
your String uses / to separate day, month, year while in your formatter you use -
your string separates hours with a dot, while in the formatter you use :
you do not have milliseconds in your string while you declared them in the formatter.
The following code should work:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh.mm.ss.S aa");
try {
Date date = dateFormat.parse("01-01-2037 01.00.00.000 AM");
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}