Convert String [] to timestamp [duplicate] - java

This question already has answers here:
Java converting a Date to a different format
(2 answers)
Closed 8 years ago.
how to convert string [] temp to Timestamp:
String[] temp = line.split(",");
for (int i = 1; i < temp.length; i++) {
objekt.setTimestamp(timestamp);}
if i use
Timestamp ts= Timestamp.valueOf(temp[0]);
objekt.setTimestamp(ts);}
i get this exceptionTimestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
if i use this
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
java.util.Date parsedDate = dateFormat.parse(temp[0]);
java.sql.Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
i get
" java.text.ParseException: Unparseable date: "06/11/2013 22:00"
thank you everyone for suggestions, i can read the timestamp but i must to read this format month-day-year: 06/18/2013 21:00:00, and with SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm"); i get 2013-06-18 18:00:00, if i change MM or dd or yyyy hier "MM/dd/yyyy HH:mm" i can't even get the month first then date and year

Your date format must actually match the text being provided. '/' and '-' do not match.
For the string you have provided, it appear the format might be "MM/dd/yyyy HH:mm".
It could also be "dd/MM/yyyy HH:mm", as it is not clear whether that date is June 11 or November 6.

Use the below code:
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");
Assuming that 06 in the string Unparseable date: "06/11/2013 22:00" correponds to month

On your 2nd code block where you got java.text.ParseException: Unparseable date: "06/11/2013 22:00, your SimpleDateFormat pattern is wrong as your String is "06/11/2013 22:00", So you have to use pattern :
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");
For more details, see java.text.SimpleDateFormat

You can do something like this :
Timestamp ts= null;
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
String[] temp = {..,..}; // it's string time array
for (int i = 1; i < temp.length; i++) {
Date convertedDate = dateFormat.parse(temp[i]);
ts = new Timestamp(convertedDate.getTime());
System.out.println(ts);
}

Related

How to format the given date "31.12.9999" in the format "yyyy/MM/dd HH:mm:ss"

i have a common doubt for a long while. During date formation if the input date in the format like "2019/05/22 02:00:23" then with the help following line we can format the date,
String inputDate = "2019/05/22 02:00:23";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date toDate = dateFormatter.parse(inputDate);
I can see both input format and the required format is same. Suppose if i change the input date like below, it is showing the java.text.ParseException: Unparseable date: exception.
String inputDate = "31.12.9999";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date toDate = dateFormatter.parse(inputDate);
Please suggest how to achieve this.
Please try below i mentioned date format code,I hope it will help you,
From this,
String inputDate = "31.12.9999";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date toDate = dateFormatter.parse(inputDate);
To Change:
Date date = new SimpleDateFormat("dd.MM.yyyy").parse("31.12.9999");
String formattedDate = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(date);
System.out.println("formattedDate :: " + formattedDate);
Thanks,.

Issue : converting String to java.util.Date

When I convert the String "07/02/2014" (mm/dd/yyy) to a java.util.Date using SimpleDateFormatter I get a result of Sun Dec 29 00:00:00 CAT 2013.
Here is the code I am running:
DateFormat formatter;
formatter = new SimpleDateFormat("MM/DD/YYYY");
Date exactDate = formatter.parse("07/02/2014");
Why is this happening ?
It must be:
DateFormat formatter;
formatter = new SimpleDateFormat("MM/dd/yyyy");
Date exactDate = formatter.parse("07/02/2014");
The documentation explains why.
y Year
Y Week year
D Day in year
d Day in month
M Month in year
m Minute in hour
Try this one,
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date exactDate = formatter.parse("07/02/2014");
When you print the Date object you will get that output. Try formatting Date into String for desired format and output.
Try following code:
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date exactDate = formatter.parse("07/02/2014");
String date=new SimpleDateFormat("MM/dd/yyyy").format(exactDate);
Hope this helps.

Unparseable date while converting to timestamp

Can anyone please clear me, what does this exception mean:
Exception in thread "main" java.text.ParseException: Unparseable date: "06.10.2013 00:00".
That was found such a date in file 06.10.2013 that cannot be parsed? must it be always 06/10/2013? I got it, when i convert String[] to Timestamp while reading file. But I have two formats in a file like 03/10/2013 and 03.10.2013, which must I use then, instead of this:
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");
String[] temp = line.split(",");
SimpleDateFormat dateFormat = new SimpleDateFormat(
"MM/dd/yyyy HH:mm" );
java.util.Date parsedDate = dateFormat.parse(temp[0]);
java.sql.Timestamp timestamp = new java.sql.Timestamp(
parsedDate.getTime());
for (int i = 1; i < temp.length; i++) {
o.setTimestamp(timestamp);
i have default both of formats in my file 06.10.2013 15:00:00 and 06/10/2013 15:00:00
Perform an analysis on the String before naively try to parse it with your current SimpleDateFormat:
SimpleDateFormat dateFormat;
//check if the string to parse contains a dot
if (stringContainingTimestamp.contains(".")) {
dateFormat = new SimpleDateFormat("MM.dd.yyyy HH:mm");
} else {
dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");
}
//rest of the code...
Another option may be just replacing the dot char by slash using String#replace(char, char):
stringContainingTimestamp = stringContainingTimestamp.replace('.', '/');
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");
//rest of the code...
The choice is yours.

convert string to date and time as am/pm format [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 8 years ago.
string : 2014-04-25 17:03:13
using SimpleDateFormat is enough to format?
or
otherwise i will shift to any new API?
Date date = new Date(string);
DateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd");
out.println( dateFormat.format (date));
My expected result is (India zone):
Date : 25-04-2014
Time : 05:03 PM
Remembering that Date objects have no inherent format, you need two DateFormat objects to produce the result you seek - one to parse and another to format:
String input = "2014-04-25 17:03:13";
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat outputFormat = new SimpleDateFormat("'Date : 'dd-MM-yyyy\n'Time : 'KK:mm a");
System.out.println(outputFormat.format(inputFormat.parse(input)));
Output:
Date : 25-04-2014
Time : 05:03 PM
Note the use of quoted sequences in the format, such a "'Date : '", which is treated as a literal within the format pattern.
I custom onTimeSet() function . Send the hour and minutes to it. It will return the time with format am and pm
public static String onTimeSet( int hour, int minute) {
Calendar mCalen = Calendar.getInstance();;
mCalen.set(Calendar.HOUR_OF_DAY, hour);
mCalen.set(Calendar.MINUTE, minute);
int hour12format_local = mCalen.get(Calendar.HOUR);
int hourOfDay_local = mCalen.get(Calendar.HOUR_OF_DAY);
int minute_local = mCalen.get(Calendar.MINUTE);
int ampm = mCalen.get(Calendar.AM_PM);
String minute1;
if(minute_local<10){
minute1="0"+minute_local;
}
else
minute1=""+minute_local;
String ampmStr = (ampm == 0) ? "AM" : "PM";
// Set the Time String in Button
if(hour12format_local==0)
hour12format_local=12;
String selecteTime=hour12format_local+":"+ minute1+" "+ampmStr;
retrun selecteTime;
}
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
more patterns you can find here
Try given below sample code:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));
//Output: 2013-05-20 10:16:44
For more functionalities on Data and Time try Joda-Time API .

How to parse String "24-10-2012" into Date ( dd-mm-yyyy) format?

I am using jdk- 1.6.
I am try to parse String "24-10-2012" date to Date (24-10-2012) but i am getting this error:
java.text.ParseException: Unparseable date: "18-11-2012"
java.text.DateFormat.parse(DateFormat.java:354)
I am parsing like this:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
Date date = formatter.parse(currentDate);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
System.out.println(formatter.parse(currentDate));
prints
Wed Oct 24 00:00:00 CEST 2012
Your problem cannot be reproduced with the code you have posted.
My hypothesis: your exception is thrown from a piece of code other than the one you are accusing of the error. You could try carefully analyzing the stack trace in order to track down the real culprit.
Date in java does not hold any format. Read more...
When I run
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
Date date = formatter.parse(currentDate);
System.out.println(date);
System.out.println(formatter.format(date));
I get
Wed Oct 24 00:00:00 BST 2012
24-10-2012
which is as I expected. Can you clarity what the problem is?
You can use this for the format "dd-mm-yyyy"
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
Date date = formatter.parse(currentDate);
System.out.println(formatter.format(date));
import java.util.Date;
import java.text.SimpleDateFormat;
public class SimpleFormatDate
{
public static void main(String args[]){
Date todaysDate = new java.util.Date();
// Formatting date into yyyy-MM-dd HH:mm:ss e.g 2008-10-10 11:21:10
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = formatter.format(todaysDate);
System.out.println("Formatted date is ==>"+formattedDate);
// Formatting date into yyyy-MM-dd e.g 2008-10-10
formatter = new SimpleDateFormat("yyyy-MM-dd");
formattedDate = formatter.format(todaysDate);
System.out.println("Formatted date is ==>"+formattedDate);
// Formatting date into MM/dd/yyyy e.g 10/10/2008
formatter = new SimpleDateFormat("MM/dd/yyyy");
formattedDate = formatter.format(todaysDate);
System.out.println("Formatted date is ==>"+formattedDate);
}
}
output
Formatted date is ==>2008-10-10 13:03:54
Formatted date is ==>2008-10-10
Formatted date is ==>10/10/2008
Wait a second.. Why u need to parsing that if u have a right value ?
Anyway, i use this :
SimpleDateFormat oFormat = new SimpleDateFormat("yyyy-MM-dd");
String sDate = oFormat.format("24-10-2012");
it will appearing date like 2012-10-24. So if u want to parsing to dd-MM-yyyy, u just need change the format to what u want.
NB : Sorry if my english is bad. :D

Categories

Resources