Cannot convert string to date
Hey Guys,
I'm starting powershell and I'm having a headheck (I'm searching for hours) with a piece of code :)
I'm trying to do that :
$LogLines = (Get-Content -Path $LogPath)
Foreach ($LogLine in $LogLines) {
$LogSubString = $LogLine.Substring(21,15)
[datetime]$date = $LogSubString
But I have an error
Cannot convert value "10-22-2014" to type "System.DateTime". Error: "String was not
recognized as a valid DateTime."
As you see, the value I read from a file is "10-22-2014".
When I try this, it is working :
$LogLines = (Get-Content -Path $LogPath)
Foreach ($LogLine in $LogLines) {
$LogSubString = "10-22-2014"
[datetime]$date = $LogSubString
So I don't understand why it's not working since "10-22-2014" and $LogLine.Substring(21,15) represents the same value but one is not working.
Can someone please help me ? :)
Cheers,
Jeremie
Get-Content has a parameter -Encoding UTF-8
But I not sure that's the way to go, that could result in data loss / confusion
Or take a good look at the string, leave $LogLine.Substring(21,15)
as is.
$LogLines = (Get-Content -Path $LogPath)
Foreach ($LogLine in $LogLines) {
$LogSubString = $LogLine.Substring(21,15)
"LogSubString=$LogSubString"
"LogSubString.Length=$($LogSubString.Length)"
FOR ($I=0;$I -LT $LogSubString.Length;$I++)
$Char = $LogSubString.Chars($I)
"I=$I Char=$Char Int=$($Char -as [Int])"
[datetime]$date = $LogSubString
Similar Messages
-
Convert String to Date and Format the Date Expression in SSRS
Hi,
I have a parameter used to select a month and year string that looks like: jun-2013
I can convert it to a date, but what I want to do is, when a user selects a particular month-year (let's say "jun-2013")
I populate one text box with the date the user selected , and (the challenge Im having is) I want to populate a text box next to the first text box with the month-year 2 months ahead. So if the user selects
jun-2013 textbox A will show jun-2013
and textbox B will show aug-2013..
I have tried:
=Format(Format(CDate(Parameters!month.Value ),
"MM-YYYY" )+ 2 ) -- But this gives an error
This returns the month in number format like "8" for august...
=Format(Format(CDate(Parameters!month.Value ),
"MM" )+ 2 )
What is the proper syntax to give me the result in this format = "aug-2013" ???
Thanks in advance.
MC
M CollierYou can convert a string that represents a date to a date object using the util.scand JavaScript method, and then format a date object to a string representation using the util.printd method. For more information, see:
http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/JS_API_AcroJS.88.1254.html
http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/JS_API_AcroJS.88.1251.html
In your case, the code would be something like:
var sDate = "2013-01-10";
// Convert string to date
var oDate = util.scand("yyyy-mm-dd", sDate);
// Convert date to new string
var sDate2 = util.printd("mm/dd/yyyy", oDate);
// Set a field value
getField("date2").value = sDate2;
The exact code you'd use depends on where you place the script and where you're getting the original date string, but this should get you started. -
I want convert string to date format in Oracle
Dear All
I want convert string to date format in Oracle,Format is given below
'Friday, 02 March 2012 2:44 PM' to '02/03/2012 2:44 PM'>
Hi Parwez,
I want convert string to date format in Oracle,Format is given below
'Friday, 02 March 2012 2:44 PM' to '02/03/2012 2:44 PM'SELECT TO_DATE('Friday, 02 March 2012 2:44 P.M.', 'DAY, DD MONTH YYYY HH:MI A.M.') from dual;
As well as what the other poster suggested, look here: http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34924
HTH,
Paul... -
Warning "Cannot convert string" - linux jdk1.4
when launching apps in jdk1.4 i get the following warnings: (any ideas anyone)
Warning: Cannot convert string "<Key>Escape,_Key_Cancel" to type VirtualBinding
Warning: Cannot convert string "<Key>Home,_Key_Begin" to type VirtualBinding
Warning: Cannot convert string "<Key>F1,_Key_Help" to type VirtualBinding
Warning: Cannot convert string "Shift<Key>F10,_Key_Menu" to type VirtualBinding
Warning: Cannot convert string "<Key>F10,Shift_Key_Menu" to type VirtualBinding
Warning: Cannot convert string "<Key>KP_Enter,_Key_Execute" to type VirtualBinding
Warning: Cannot convert string "Alt<Key>Return,Alt_Key_KP_Enter" to type VirtualBindingThe command:
xmbind --help
gives an examplanation of what is happening:
With no FILE specified, the file .motifbind in the user's home directory
is used. If this file is not found, xmbind loads the default virtual key
bindings.
If you run:
xprop -root | grep -i bind
This will show your default bindings.
To remove the defaults run:
xprop -root -remove MOTIFDEFAULT_BINDINGS
To check it worked again run:
xprop -root | grep -i bind
To give the bindings I have created a file in my home directory called '.motifbind'. When you run your java app, if the default bindings have been removed the app will cause motif to get the bindings from the '~/.motifbind' file.
This is my file:
! ~/.motifbind file
osfActivate : <Key>KP_Enter
osfAddMode : Shift<Key>F8
osfBackSpace : <Key>BackSpace
osfBeginLine : <Key>Home
osfCancel : <Key>Escape
osfClear : <Key>Clear
osfCopy : Ctrl<Key>Insert
osfCut : Shift<Key>Delete
osfDelete : <Key>Delete
osfEndLine : <Key>End
osfHelp : <Key>F1
osfInsert : <Key>Insert
osfLeft : <Key>Left
osfMenu : Shift<Key>F10
osfMenuBar : <Key>F10
osfPageDown : <Key>Next
osfPageLeft : Ctrl<Key>Prior
osfPageRight : Ctrl<Key>Next
osfPageUp : <Key>Prior
osfPaste : Shift<Key>Insert
osfPrimaryPaste:Meta<Key>Insert
osfRight : <Key>Right
osfSelect : <Key>Select
osfUndo : <Key>Undo
osfUp : <Key>Up
osfDown : <Key>Down
When you run your app the errors hopefully will not show.
If you want to make a change to your '~./motifbind' file you will first need to remove the property, by now running:
xprop -root -remove MOTIFBINDINGS
and then running your app. -
Facing problem in converting string to date using getOANLSServices()
I am trying to set a value for date field in my vo and trying to insert into the table.
In controller I am getting the String which has a date:
ex: String date="01-NOV-2007";
while setting into the row I need to convert into Date but it is returning null.
The below code I used
to convert into date :
Date dt = new Date(getOADBTransaction().getOANLSServices().stringToDate(date));
But this dt is returning a null value is there any solution please advise me.
Regards!
SmarajeetSmarajeet ,
See this thread, in one of my replies i have mentioned how to convert string to java.sql.date.You can use the same for oracle.jbo.domain.Date.
urgent!How to set the default selected date for an OAMessageDateFieldBean
--Mukul -
Hi All,
I am having a String value which is having date in the format 'Mon Apr 14 16:07:52 IST 2008'
Now I want to convert this String to Date with format 'mm/dd/yyyy' , to display as 04/14/2008
Also I want to add 5 days to this date and display the new date 04/19/2008
I don't know How to convert the string which is having date in 'Mon Apr 14 16:07:52 IST 2008' format to format I need.
Please help me.Hi ,
I wrote a class that tries to parse a String Date and converts it to object. However, I found a very interesting thing while testing this code. I was trying to paarse "2008:03:04 16:21:05" string by using
DateFormat.getDateFromString(null,"2008:03:04 16:21:05" )
It should by pass all try loop accept the one with yyyy:MM:dd HH:mm:ss format. But it went inside the try loop which had MM:dd:yyyy HH:mm:ss format and gave me a wrong date object with year 0171, april 03.
Could any one help me to understand why it is happening? as a work around I put MM:dd:yyyy HH:mm:ss at the end of the program and it is giving me correct object as it did not have to go that far before matching the format.
Thanks,
AL-Amin
Code:
import java.text.*;
import java.util.*;
public class DateFormat
public Date getDateFromString(String format, String parseDate)
Date d;
if(format!=null) {
try
SimpleDateFormat sdf = new SimpleDateFormat(format);
d = sdf.parse(parseDate);
catch(Exception e)
System.out.println(e.toString());
d = null;
return d;
else {
try
format = "yyyy-MM-dd HH mm ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
d = sdf.parse(parseDate);
catch(Exception e)
System.out.println(e.toString());
System.out.println("The Date was not of '"+format+" ' format");
d = null;
if(d==null) {
try
format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
d = sdf.parse(parseDate);
catch(Exception e)
System.out.println(e.toString());
System.out.println("The Date was not of '"+format+" ' format");
d = null;
if(d==null) {
try
format = "yyyy-MM-dd HH_mm_ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
d = sdf.parse(parseDate);
catch(Exception e)
System.out.println(e.toString());
System.out.println("The Date was not of '"+format+" ' format");
d = null;
if(d==null) {
try
format = "yyyy-MM-dd HH-mm-ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
d = sdf.parse(parseDate);
catch(Exception e)
System.out.println(e.toString());
System.out.println("The Date was not of '"+format+" ' format");
d = null;
if(d==null) {
try
format = "yyyy-MM-dd-HH-mm-ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
d = sdf.parse(parseDate);
catch(Exception e)
System.out.println(e.toString());
System.out.println("The Date was not of '"+format+" ' format");
d = null;
if(d==null) {
try
format = "yyyyMMddHHmmss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
d = sdf.parse(parseDate);
catch(Exception e)
System.out.println(e.toString());
System.out.println("The Date was not of '"+format+" ' format");
d = null;
if(d==null) {
try
format = "MM-dd-yyyy HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
d = sdf.parse(parseDate);
catch(Exception e)
System.out.println(e.toString());
System.out.println("The Date was not of '"+format+" ' format");
d = null;
if(d==null) {
try
format = "dd-MM-yyyy HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
d = sdf.parse(parseDate);
catch(Exception e)
System.out.println(e.toString());
System.out.println("The Date was not of '"+format+" ' format");
d = null;
if(d==null) {
try
format = "MM:dd:yyyy HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
d = sdf.parse(parseDate);
catch(Exception e)
System.out.println(e.toString());
System.out.println("The Date was not of '"+format+" ' format");
d = null;
if(d==null) {
try
format = "dd:MM:yyyy HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
d = sdf.parse(parseDate);
catch(Exception e)
System.out.println(e.toString());
System.out.println("The Date was not of '"+format+" ' format");
d = null;
if(d==null) {
try
format = "yyyy:MM:dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
d = sdf.parse(parseDate);
catch(Exception e)
System.out.println(e.toString());
System.out.println("The Date was not of '"+format+" ' format");
d = null;
if(d==null) {
System.err.println("None of the Date format worked. Please modify the calling method and provide a specific format");
return d;
} -
Problem in Converting String to Date
Hi All,
I am having one String
String date = "2006-01-17 15:19:57.0"
I want to parse this String into Date object.
I will really appriciate if somebody helps me out.
Thanks.You're specifying a 'T' and a timezone in your format, but they're not present in the string you're parsing.
I'm assuming from the way you're printing out the date, that your thinking is along these lines: "sdfInput will parse the input string, no matter what format it's in, and will produce a Date object. That Date object wil have the format specified in sdfInput."
This is wrong on a couple of fronts:
1) DateFormat doesn't magically figure out what format it's supposed to use for the String it's parse()ing. The String has to match the DF's format.
2) Dates don't have formats. Only Strings do. A Date object is just a long. There's no relationship whatsoever between the Date that you get from parse() and the format that was used to produce it. When you print out a Date as you're doing, its toString method is called, which in turn uses a default format for your Locale.
If you want to turn a date string in one format into a date string in another format, use two different DateFormat objects with two different formats. Date date = df1.parse(inputString);
String outputString = df2.format(date); -
Need info on SimpleDateFormat, Converting String to Date
I'm a newbie and doing a conversion of a string to a date and it's adding a little less than 11 minutes. I know I'm missing something really simple but as I read it I ought to be able to convert "20030125 084539.637696" to Sat Jan 25 08:45:39 not 8:56! Also, for the time being I'm ignoring the zulu 'Z' because I can't find a pattern that'll take it.
System.out.println("INFO:MetadataExtractorBean::filestarttime:" + filestarttime);
filestarttime = filestarttime.replace("Z","");
System.out.println("after filestarttime.replace(Z,null) filestarttime:" + filestarttime);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd hhmmss.SSSSSS");
Date convertedDate = dateFormat.parse(filestarttime);
System.out.println("after dateFormat.parse(filestarttime) convertedDate:" + convertedDate);
INFO:MetadataExtractorBean::filestarttime:20030125 084539.637696Z
after filestarttime.replace(Z,null) filestarttime:20030125 084539.637696
after dateFormat.parse(filestarttime) convertedDate:Sat Jan 25 08:56:16 EST 2003
Can someone help me with a) why it doesn't remain 8:45, and b) how to modify the pattern to be able to parse a zulu date. Thanks in advance.import java.text.*;
public class ParsingExample {
public static void main(String[] args) throws ParseException {
String s = "20030125 084539.637";
SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd HHmmss.SSS");
System.out.println(f.parse(s));
}Your round up was because ".637696" was taken to mean 637,696 milliseconds. SimpleDateFormat doesn't have a way to accept microseconds, and anyway, java.util.Date only has millisecond precision. I also changed "hh" to "HH" because I assume this is with a 24 hour clock.
edit: you can tell your date format not to do these roll ups by setting:
f.setLenient(false); -
How to convert String to date ? - MS access
I want to convert a string "10-10-08" [ MM-dd-yy ] into date type in same format to use that in query statement [MS Access].
In table , I am using field called "date" with "MM-dd-yy" format.
Example:
<%@ page import= "java.sql.*"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="java.util.Date"%>
<%
String d="10-9-08";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yy");
Date date = dateFormat.parse(d);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:bspipdb");
Statement st = con.createStatement();
ResultSet rs=st.executeQuery("Select * from datacollectdb where date='"+date+"'");
%>
I am getting "java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.
" error.
How to use the string "10-10-08" in where statement?sangee wrote:
I want to convert a string "10-10-08" [ MM-dd-yy ] into date type in same format to use that in query statement [MS Access].
In table , I am using field called "date" with "MM-dd-yy" format.I think a column named "date" is a very bad idea. Not just because "date" is a keyword. It's not very self-describing. How about "birth_date" or "hire_date" or "went_on_my_first_date"?
Example:Scriptlets? There's your second mistake.
<%@ page import= "java.sql.*"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="java.util.Date"%>
<%
String d="10-9-08";The way you're doing things, "2-40-99" is a valid date. Is that what you want?
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yy");
Date date = dateFormat.parse(d);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:bspipdb");Wrong again. Better to use a PreparedStatement and bind that Date.
Statement st = con.createStatement();
ResultSet rs=st.executeQuery("Select * from datacollectdb where date='"+date+"'");
%>
I am getting "java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.
" error.
How to use the string "10-10-08" in where statement?Use PreparedStatement and bind.
Your JSP should not be getting a database connection. Pool it.
Your code is wrong on every level. You don't close resources, you don't separate you app into layers, your database code is not in a POJO. I'd recommend that you throw this away and start again.
% -
HI Everyone,
i am trying to convert a date which is in string format to a date format...but i am having this error
javax.servlet.ServletException: Unparseable date: "2004-05-03"
Can anyone help?
Here is a piece of code :-
SimpleDateFormat formatter= new SimpleDateFormat ("yyyy-mm-dd");
Date today = formatter.parse(fulldate);You need to change your formatting string to: "yyyy-MM-dd" Lower case 'm' is for minutes...
-
Convert string to date for Ms Access
I have some date in a textFile!!!
I get them via Buffered and FileReader into my java application and put them into a Vector!
Every date is formatted as Strings and I want to convert them into a date format(YYYYMMDD).
The date are not used for " System.out.println", but I want to insert them into a Access-database(use a jdbc/odbc bridge).
The Access database seems only to accept YYYY-MM-DD, the only formatting way is how you choose to "show" the dates (short, long etc) but anyway....(:-)
I�ve tried following example with format.getTime() and it works. But I want to insert the date from
my textfiles instead.
Here some examples from my code with the getTime()method!!!
String str = String.valueOf(fields.get(3));
String ar = str.substring(0,4)+"-";
String m = str.substring(4,6)+"-";
String d = str.substring(6,8);
String datum = ar+m+d; //this is my date in a String format!!!
Calendar calendar = new GregorianCalendar();
Date date = calendar.getTime();//this objectref. takes the sys.date i want my string date instead(:-)
DateFormat localFormat = DateFormat.getDateInstance();
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd" );//the only format for Access I think!
for (int j=0; j<(fields.size()-1); j++){//pos. from my vector
sql = sql + "'" + fields.get(j) + "'" + "," ;
sql = sql + "'" + format1.format( date ) "'" ; //the last field i the db
db myTest = new db();
myTest.executeDML("insert into tblBooks values "+sql+")");
Sorry for my "bumpy" English(:-) And thanks! EvaIf you tell us what the format if the dates in the file is, we might be able to suggest something :)
-
Converting string to date in mm/dd/yyyy format
Hi,
I used simpledateformat to convert date to "MM/dd/yyyy" format.
Now when i want to insert into db, i want the value as date object instead of string [returned by format method of SimpleDateFormat]
what should i do?
Date dt = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
System.out.println("Formatted date is coming as "+sdf.format(dt));
Date dt1 = sdf.parse(sdf.format(dt);
System.out.println("parsed date is coming as " +dt1);
output :
1 SOP returns 11/07/2007 [current date]
2 SOP returns Wed Nov 07 00:00:00 GMT+05:30 2007If you are using PreparedStatements setDate method, you need a java.sql.Date object, not a Java.util.Date object. You can create an sql.Date object from a util.Date object as follows:
java.sql.Date sqlDate = new java.sql.Date(dt.getTime());And, if you are not using PreparedStatement and its setDate method, then change now.
Doing
Date dt1 = sdf.parse(sdf.format(dt));is meaningless. You format the date to string, then parse the string back to a date? Why? Why not just use the original Date object? And you won't get a sql.Date object that way, either, as the DateFormat classes return a util.Date object.
Edit: And for more info:
A Date does not have a format. It is simply a number. The "format" only comes into play when printing the String representation of the Date, and that is not handled by the Date object itself (except when calling toString either explicitly or implicitly). -
How to convert string to date format?
Hi All,
String is in following format: 2006-12-07 i.e yyyy-mm-dd
I want it in 07.12.2006 date format.
which function module should i used to convert it?
Regards,
Nilimaan other way to do this :
Code
DATA :
ld_text(20) TYPE c,
ld_date TYPE datum.
ld_text = '2006-12-07'.
REPLACE ALL OCCURENCES OF '-' IN ld_text WITH ''.
WRITE ld_text TO ld_date.
WRITE ld_date. -
How to convert string into date datatype
hi! there
i've a date in string format like MM/dd/yyyy
eg:String sDate = "01/30/2002";
and i want to convert this string format into java date format
same as 01/30/2002 only
when i write like this
SimpleDateFormat formatter = new SimpleDateFormat ("MM/dd/yyyy");
String sDate = "1/11/2002";
java.util.Date oDate = formatter.parse(sDate);
i'm getting the output
Fri Jan 11 00:00:00 GMT+05:30 2002
i just want the out put like 01/30/2002
plz,help meHi,
Just use back the SimpleDateFormat object you defined.
String myDateInStr = formatter.format(oDate);
this will format a java.util.Date object to a string representation according to the format you specified in the instantiation of SimpleDateFormat object. -
How to convert string into date
Hi all,
I m new to JDeveloper
I m taking the date as a string then passing it as a string in the method.
I want to convert tht string into the date of format (DD-MON-YYYY) or any other format
require yur assistance in this job.
THANKXX in advance
ankitIs this what you are looking for:
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("dd-MMMMM-YYYY");
java.util.Date date1 = null;
try
date1 = formatter.parse("Thu Jun 3 09:09:30 2004");
catch(Exception e)
e.printStackTrace();
System.out.println("Date1 in millis : " + date1.getTime());
System.out.println("Date1 in string : " + date1);
java.util.Date date2 = new java.util.Date(date1.getTime());
System.out.println("Date2 in string : " + date2);or look at http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html for more options
Maybe you are looking for
-
Why the bill desk a payment gate way does not open?
I am from India and on personal social visit to US. I am staying with my daughter in broofield Milwaukee. I need to pay my credit card bills. The credit card directs the payment through gateway provided by bill desk/paynet of india. Hoever, after Jun
-
Here's the thing .... say I need to find a contact within a particular company but I don't know the contacts actual name .... say I need to find the HR manager at Company X. I do a search on Company X .... I get a list of contacts that I have created
-
Help with installation of Adobe Acrobat 10
I installed the adobe cs5.5 premium on my laptop and it was fine, I got a virus lost everything and had to restore my computer back to factory settings. I reloaded Cs5.5 and everything loaded except Acrobat. I have tried to do it in safe mode, take
-
HT201210 i can not restore my ipone in itunes erro (3014)
i can not restore my ipone in itunes
-
Applespell.crash.lot
I've started getting the error message that Spell Checker can't be located. I've been reading a lot of threads and I think my AppleSpell is broken. I find the application, but it doesn't show in the Activity Monitor and when I Spotlight it, all I fin