Getting Computer Date in java

Ads
 

Getting Computer Date in java

Hi , I want to get the PC Date and insert it to a DB, specifically MSSQL.. Everything in my scriptlet works but this..

String strDate = new Date();

I get this server error, "The type Date is ambiguous".

I've also tried Date date = new Date(); ... but it get's the same error.

Also, I want the date format to come out something like this.. 10-24-2012..

Can anyone help??

View Answers

February 28, 2012 at 5:55 PM

date datatype should be of varchar type.

import java.sql.*;
import java.util.*;
import java.text.*
class  InsertDate{
    public static void main(String[] args){
    try{
        java.util.Date d=new java.util.Date();
        SimpleDateFormat sdf=new SimpleDateFormat("MM-dd-yyyy");
        String st=sdf.format(d);
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root" );

        Statement pstmt = conn.createStatement();
        String INSERT_RECORD = "insert into person(dob) values('"+st+"')";
        pstmt.executeUpdate(INSERT_RECORD);
        pstmt.close();
        conn.close();
    }
    catch(Exception e){
        e.printStackTrace();
    }
  }
}

February 28, 2012 at 6:03 PM

The package java.util.* and java.sql.*, both are having Date class. Therefore this error occurs. Use classes with their package as well like if you are using Date class of util package then use it as java.util.Date and if you are using java.sql.Date then use it as java.sql.Date.


February 28, 2012 at 6:41 PM

Thanks ! Helped Very Much ! :D

Ads









Related Tutorials/Questions & Answers:
Getting Computer Date in java
Getting Computer Date in java  Hi , I want to get the PC Date... but this.. String strDate = new Date(); I get this server error, "The type Date is ambiguous". I've also tried Date date = new Date(); ... but it get's the same error
Date not getting in proper format
Date not getting in proper format  import java.util.*; class Date { public static void main(String[]args) { Date d = new Date(100000000000L); System.out.print("1st date"+d.toString()); } } out put:- 1st date@3e25
Advertisements
Get computer name in java
Get computer name in java       We can get the computer name by the java code program. For getting computer name we have used java.net.InetAddress class. We will use static
Java Date
of Java Date at roseindia.net: Java Date Examples Getting time in Milliseconds...Java Date  Hi, What is the API for Date management in Java? How use the Java Date API for manipulating date object in Java? Provide some
Getting the current date
Getting the current date     ... date of the system. In this section, through the given program you can get the current date in the proper format (Day/Month/Year). This program shows
java with computer network
java with computer network  i feel it's very difficult to use java for network programming. how can i recover this problem?give me any simple example... that explains the java for network programming Overview of Networking through JAVA
Getting the modification date and time of file or folder in Java
C:\nisha>java GetDirectoryAndFileModifiedTime Enter file or directory name in proper format to get the modification date and time : myfile.txt File name : myfile.txt File modification date
Getting Started With Java
Getting Started With Java       JDK 6 Tutorial The latest news for the java programmers that the Sun MicroSystems has released the Java SE 6 on Monday December 11.  
getting random number in java
getting random number in java  getting random number in java Hi... random numbers between 1 to 100 in Java. Though i m new to Java, i have tried many... in my Java application but nothing seems to work. In order to get the random
How to compare date in Java?
How to compare date in Java?  Hi, How to compare date in Java? Thanks   Hi, Check this example Date Comparison. Thanks
Computer forensic application development using JAVA program
Computer forensic application development using JAVA program  Write a computer forensic application program in Java for Recovering Deleted Files and Deleted Partitions
Getting A File's Mime Type In Java
Getting A File's Mime Type In Java  Getting A File's Mime Type In Java
Getting date by time
Getting date by time       In this example of creating date using the timestamp value. We can... DateByTime.java C:\DateExample>java DateByTime New Date:=Sun Jan 25
Mysql Date To Java Date
Mysql Date To Java Date       Mysql Date To Java Date retrieve the records from Mysql and display the date in java. Understand with Example The Tutorial illustrate an example from
how to add date and change date in java - netbeans
how to add date and change date in java - netbeans  I need to code use to increase date How to get date after 30 days using netbeans
Getting Previous, Current and Next Day Date
Getting Previous, Current and Next Day Date   ... previous, current and next date in java. The java util package provides... the previous, current and next date to Date() constructor of java.util package
Parsing date in Java
Parsing date in Java  How can i parse the date in Java in a simple format..?   SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy
java format date
java format date  Hi, How can I format the date in the following...) { Date date=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); String formattedDate=sdf.format(date
Date picker in Java Swing
Date picker in Java Swing  Hello Sir, Sir ,I am in need of Time Picker just like the Date picker available in java Swing. Kindly... link: Java Date Time Picker Example   Hello Sir, Thank
Java String To Date
Java String To Date In this section you will read about how to convert... be convert into java.util.Date. Java string to date convert explains the conversion of Java string written into the specified format to a Java date object
Getting environment variable value from java
Getting environment variable value from java  Getting environment variable value from java
date and time in awt(java)
date and time in awt(java)  sir, do you have an example of date in awt java which can be view over a textfield.   Display time in JTextField import java.util.*; import javax.swing.*; import java.awt.event.*; public
How to get the current date in Java?
How to get the current date in Java?  Hi, I am new to Java... in Java? Thanks   Hi, Check the tutorial: Java program to get current date. Thanks   Hi, To get the current date and time in Java you can use
date picker componnt - Java Beginners
date picker componnt  sir i need code for date picker in java swing for my project sir please send me sir
Java Date Example
Java Date Examples       Getting astrological sign according to birth date In this section we will be getting the astrological sign according to the inserted birth
How to calculate Date Difference in Java?
How to calculate Date Difference in Java?  Hi, I have two dates variable. How to calculate Date Difference in Java? Thanks   Hi, You can use the Calendar class for calculating the date difference in Java. Get data
Java Date Class
Java Date Class  Hi Friend, I need to have a code to get the days between specific dates. Such if I enter 19 September 2011 to 21 September 2011, the days would be saturday, sunday and monday, the code I gave simply get one day
java date selector ..
java date selector ..  hey,,,, i am creating my java project...for that i implementing a car renting system and i had used combo boxes to get Ranting start day inputs..3 combo(sy,sm,sd) yyyy ,mm,dd and for returning day i need
Java current date - How to get current date in Java
In this section, you will learn about displaying current date and time in Java
How to convert date to string in Java?
How to convert date to string in Java?  Hello developers, I have java.util.Date object and it has to be converted to date format. How to convert date to string in Java? Thanks   Hi, To convert java.util.Date object
date
date  can u tell me how to calculate difference between a user provided date and the system date in java

Ads