Grabbing the current time?

I know some Javascript, and once I got an Arduino error from java.util. something, so Arduino code does have some Javascript in it. I want to know if there's a way to do something based on the current time or date, without having to modify and re-upload the code every time the Arduino resets. In Minecraft Mods Programming Absolute Beginner's Guide by Rogers Cadenhead, he does this in the NetBeans compiler on pages 100-101 by doing this (line numbers shown):

3:import java.time.*;
4:import java.time.temporal.*;
5:
6:class Clock {
7:   public static void main(String[] arguments) {
8:      // get current time and date
9:      LocalDateTime now = LocalDateTime.now();
10:     int hour = now.get(ChronoField.HOUR_OF_DAY);
11:     int minute = now.get(ChronoField.MINUTE_OF_HOUR);

and then continuing with his code. In Arduino, however, even what he does on lines 3-4 doesn't work because the keyword import should be #include. If that's the case, then how do I know if the rest of the code (lines 9,10, 11) will work? even Javascript System.out.println("Good morning. \n"); is Arduino Serial.println("Good morning. \n");. This kind of problem isn't even mentioned in Make: Getting Started with Arduino, 3rd Edition.
Note: I am using IDE 1.6.4.

Javascript won't run in an Arduino. You need to write C++.

Do you have an RTC connected to the Arduino? That's the best way to get the current date/time.

Or are you trying to get the PC's date/time? If so, you need an app running on the PC that sends the information, then you use the Arduino to read it from the serial port.

so Arduino code does have some Javascript in it

Not a bit. The IDE is written in Java.

I want to know if there's a way to do something based on the current time or date, without having to modify and re-upload the code every time the Arduino resets.

Add a real time clock - RTC. Dirt cheap and fairly accurate.

Both of you suggested an RTC. Is that part of the code or an external device?

bbrk24:
Both of you suggested an RTC. Is that part of the code or an external device?

It's an external device - a "Real Time Clock" module. Do a Google search for more info. <$5 each

OldSteve:
Javascript won't run in an Arduino. You need to write C++.

Javascript is based on C++, and with a few exceptions (such as the #include keyword on the size of longs and ints outside the Due) is mostly the same.

OldSteve:
It's an external device - a "Real Time Clock" module. Do a Google search for more info. <$5 each

If I get one, how do I hook it up?
Google searching "Real-Time Clock" gives 6:33 PM (the current time) and some other unhelpful links.

http://playground.arduino.cc/code/time

https://www.google.com.au/search?q=rtc+module+arduino&ie=utf-8&oe=utf-8&gws_rd=cr&ei=qt5cVqKaIKPKmwXzzL3ACA#

According to this page,

 hour();            // The hour now  (0-23)
 minute();          // The minute now (0-59)
 second();          // The second now (0-59)
 day();             // The day now (1-31)
 weekday();         // Day of the week, Sunday is day 1
 month();           // The month now (1-12)
 year();            // The full four digit year: (2009,
                    //  2010 etc)

are all functions. It might me because I'm using IDE 1.6.4, but they don't turn orange and aren't recognised by the compiler. The page says that they don't work in 1.6.1, but it doesn't say anything about 1.6.4.
EDIT: the example code

#include <Time.h>  

#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by Unix time_t as ten ASCII digits
#define TIME_HEADER  'T'   // Header tag for serial time sync message
#define TIME_REQUEST  7    // ASCII bell character requests a time sync message 

// T1262347200  //noon Jan 1 2010

void setup()  {
  Serial.begin(9600);
}

void loop(){    
  if(Serial.available() ) 
  {
    processSyncMessage();
  }
  if(timeStatus() == timeNotSet) 
    Serial.println("waiting for sync message");
  else     
      digitalClockDisplay();  
  delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void processSyncMessage() {
  // if time sync available from serial port, update time and return true
  while(Serial.available() >=  TIME_MSG_LEN ){  // time message consists of header & 10 ASCII digits
    char c = Serial.read() ; 
    Serial.print(c);  
    if( c == TIME_HEADER ) {       
      time_t pctime = 0;
      for(int i=0; i < TIME_MSG_LEN -1; i++){   
        c = Serial.read();          
        if( c >= '0' && c <= '9'){   
          pctime = (10 * pctime) + (c - '0') ; // convert digits to a number    
        }
      }   
      setTime(pctime);   // Sync Arduino clock to the time received on the serial port
    }  
  }
}

reports the error

Arduino: 1.6.4 (Windows 7), Board: "Arduino Uno"

Build options changed, rebuilding all

sketch_nov30a.ino:1:20: fatal error: Time.h: No such file or directory
compilation terminated.
Error compiling.

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

"Error compiling." Helpful.

Can't you read this!

Update: newer versions of Time, TimeAlarms, and DS1307RTC are available, featuring more updates for newer version of Arduino and compatibility with Arduino Due.
The code is derived from the earlier Playground DateTime library but is updated to provide an API that is more flexible and easier to use.

This old download does not work on Arduino 1.6.1. Use the links above.

BillHo:
Can't you read this!

Update: newer versions of Time, TimeAlarms, and DS1307RTC are available, featuring more updates for newer version of Arduino and compatibility with Arduino Due.
The code is derived from the earlier Playground DateTime library but is updated to provide an API that is more flexible and easier to use.

This old download does not work on Arduino 1.6.1. Use the links above.

I didn't really understand what that meant. All I got was that it "does not work on Arduino 1.6.1."

bbrk24:
I didn't really understand what that meant. All I got was that it "does not work on Arduino 1.6.1."

Does Mummy also still need to tie your shoes?
Spend some time reading the documentation, install the correct libraries, and everything will work fine.

"Error compiling." Helpful.

The helpful part is:

fatal error: Time.h: No such file or directory

So apparently you do not have file Time.h in the right place.

bbrk24:
I didn't really understand what that meant. All I got was that it "does not work on Arduino 1.6.1."

It said the old download does not work on Arduino 1.6.1. Use the links above.
There is updated version that work with Arduino 1.6.4
This is a add on library, you need to download and install it before you can use it.

http://www.arduino.cc/en/Reference/Libraries

http://bildr.org/2011/03/ds1307-arduino/

works also for the DS3231. No library needed.

Also, I suspect the only time you actually need a clock is when you are just making a stand-alone clock, or recording data to a local SD card. Anything else means you can use somebody else's clock.

OldSteve:
Does Mummy also still need to tie your shoes?
Spend some time reading the documentation, install the correct libraries, and everything will work fine.

I don't actually program very much.
I am (or at least was) much more active on scratch.
I can't even figure out how to change my profile picture on this website!

I can't change my profile stuff either. I think some features got disabled as part of the anti-spam efforts.

bbrk24:
I don't actually program very much.
I am (or at least was) much more active on scratch.
I can't even figure out how to change my profile picture on this website!

You don't need to change your profile picture. Worry about your code for now, and that later.

I'm running Arduino V1.6.5, have never used an RTC with an Arduino, but just downloaded and installed all three libraries - "DS1307", "Time" and "TimeAlarms". The "TimeRTC", "TimeAlarmExample" and the DS1307 "ReadTest" examples all compile without problems.
So as long as you install the latest version of the libs, you should have no dramas.

Then just do plenty of reading in the documentation.

Edit: I changed my profile pic last week, after I eventually found how to do so. It's not too clear. I'll have a look now, to refresh my memory on how I did it.

Edit2: Click on "Profile" at the top of the page, then on the page that opens: "Upload new photo". It's the first thing you see. I missed it because I kept going straight past without seeing it. :blush:

BillHo:
It said the old download does not work on Arduino 1.6.1. Use the links above.
There is updated version that work with Arduino 1.6.4
This is a add on library, you need to download and install it before you can use it.

http://www.arduino.cc/en/Reference/Libraries

I did download the Time library.

CrossRoads:
The helpful part is:

fatal error: Time.h: No such file or directory

So apparently you do not have file Time.h in the right place.

And I put it in the libraries folder. I still get the error.
EDIT: I restarted the IDE. It works. If by "works" you mean "spams 'waiting for sync message'."

bbrk24:
I did download the Time library.And I put it in the libraries folder. I still get the error.

The library folder is inside another folder of the same name. It needs to be removed from that second folder, so that "Time.h" is in the top-level folder. Same goes for "TimeAlarms" and "DS1307".
I downloaded to my Desktop, extracted, then opened each folder in turn and copied the inner folder to my "\Sketchbook\libraries" folder, and everything worked perfectly. Downloading, installation and compiling the examples took less than 5 minutes.

Edit: I guess I should buy a couple of RTC modules now. :smiley:
Done. AU$1.09 each delivered here:- for Arduino AVR Arm Pic RTC I2c Ds1307 At24c32 Real Time Clock Module for sale online | eBay

bbrk24:
I did download the Time library.And I put it in the libraries folder. I still get the error.
EDIT: I restarted the IDE. It works. If by "works" you mean "spams 'waiting for sync message'."

You need to use the Serial Monitor from the IDE and type this following to set the clock

T1448931002