Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Current Date and Time to a String

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Current Date and Time to a String

    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Scanner;


    public class Swipe_In
    {
    public String DTStart; //Swipe in Date Time


    public static void main()
    {

    //Swipe_In demo = new Swipe_In();

    System.out.println("Press 1 for Swipe In ...");

    Scanner flagIn = new Scanner(System.in);
    int checkIn = flagIn.nextInt();

    if (checkIn==1)
    {
    System.out.println("You have successfully Swiped In...");

    //current(Swipe In) Date & Time as DTStart
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Calendar cal = Calendar.getInstance();
    DTStart = dateFormat.format(cal.getTime()); //acquires the current time and date...


    System.out.println("Swiped In @ " + DTStart); // <--error here ...

    }
    else
    {
    System.out.println("Swipe In Unsuccessful ...Try Again");
    }


    }


    }




    I have got 2 questions on the above class...

    1) I get an error "Cannot make a static reference to the non-static field DTStart" wherever i use DTStart in the main
    method....What's causing this error? and How do i fix it for good?

    2) public String DTStart;
    Once i get the current date & time assigned to the above DTStart String in the main method i cannot use the updated
    value of DTStart in any other class. In another class i wanna create an instance(object) of this Swipe_In class with
    DTStart assigned to the current time(as in its main method)..I tried but the object's DTStart gets assigned to null.((
    How do i achieve this? Hope i haven't twisted my last question too much..

    I would really appreciate your time and effort if u can help me with my prob....Thank u ...)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Current Date and Time to a String

    Cannot make a static reference to the non-static field DTStart
    Make it static


    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Using Date() to get Start Time and Finish Time of a copyFiles method
    By dalythe in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 17th, 2013, 09:50 PM
  2. Replies: 1
    Last Post: July 22nd, 2011, 07:08 AM
  3. [SOLVED] how to get each field of a current date
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: January 24th, 2010, 06:33 AM
  4. How to Get the current date and time
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 2nd, 2008, 01:55 PM
  5. How to Get the current date and time
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: December 2nd, 2008, 01:55 PM