Writing date and time in a log file in java


    Sponsored Links


  • 1. Defeating Popup Stoppers please?
    Hello Programmers, I am looking for either Java Script (OR HTML etc) to DEFEAT Pop-up Stoppers e.g It will bring up a window that will LOOK like a Pop-up FEEL like a Pop-up Allow a name and e-mail entry BUT is not affected by Popup Stopper, PopUp Killer etc Bill
  • 2. Dynamic Object Names?
    I'm working on a simple app that will manage bank accounts. I have written an Account class with doubles savings and checking and all the necessary accessor methods. I'd like to give the user the ability to create new accounts without writing new apps. Unfortunately, I was told there is no way to create a new object with a user-defined string as it's name. So: Is there any way I can take string input from the user and then create a new Account named whatever the string is? I thought about the possibility of putting a name(string) attribute in the Account class and creating a method that takes a string and searches through an array of Account objects until it finds the one with the right name... Then I realized that even if I did that, all the Accounts would disappear after the program ended.. noooooo.. So, even though I can't really use "dynamic object names"(i dunno what they're really called) for that app, I'd still like to know how to do them. After I realized that problem, I decided each Account object would have a File object (a text file) that kept track of the money. Although no bank will ever use my app, I'd still like to know if it's possible to prevent outsiders from simply editing the text files to manipulate the balances. Is there any way to store data from one program execution to another without making the data vunerable to greedy little punks? Whew-Thanks for your help!
  • 3. error when using native2ascii: java.lang.NumberFormatException: npos
    I'm trying to convert some properties files from UTF8 into ISO8859-1 encoding. here's the syntax I'm using: $ native2ascii -reverse -encoding ISO8859_1 orig.txt new.txt and I get this error: java.lang.NumberFormatException: npos I was trying to find some documentation of this error, but couldn't find it. The syntax above works for some files, but not for others. The German properties files are the ones I have problem with. Any help greatly appreciated - thanks.
  • 4. [Help] me on choosing Java Bytecodes Obfuscator tools
    Dear folks, Now I need a Obfuscator tool,and I've used some,like RetroGuard,JAX,etc. I'd like to listen to your better choice with the reason why,thanks a lot. hoop

Writing date and time in a log file in java

Postby christopher_board » Mon, 04 Sep 2006 23:17:10 GMT

Hi. I am writing an HTML log file in Java. I was wondering if I could
do it as

import java.util.*;
out.write("" + new Date());

However this comes up with an error. I was wondering how this would be

Any help would be appreciatated. 

Thanks very much for your help

Re: Writing date and time in a log file in java

Postby Andrew Thompson » Tue, 05 Sep 2006 00:01:05 GMT

What do you mean by an 'HTML log file'?
Is that a log file formatted as a web-page?

In what sort of class are you doing this?
Your code suggests to me a JSP or servlet.

My crystal ball is broken.  Care to share what
that error is?  (Please always quote error
messages word for word - copy/paste is best)

For a basic class, it might be..

import java.util.*;

class TestDate {
  public static void main(String[] args) {
    System.out.println(new Date());

Andrew T.

Re: Writing date and time in a log file in java

Postby christopher_board » Tue, 05 Sep 2006 02:44:57 GMT

The code that i have done is

import java.util.*;

try {
            BufferedWriter out = new BufferedWriter(new FileWriter(
                    "Logs.html", true));
            out.write("" + new Date());
catch (IOException e)


However this doesn't work because it comes up with the error:
"Logon.java": reference to Date is ambiguous, both class java.util.Date
in java.util and class java.sql.Date in java.sql match at line 248,
column 40

Re: Writing date and time in a log file in java

Postby Andrew Thompson » Tue, 05 Sep 2006 03:21:48 GMT

Yes.  That's good, but please do not send me email!

(and please refrain from top-posting..)

Andrew T.

Re: Writing date and time in a log file in java

Postby Ralf Seitner » Tue, 05 Sep 2006 03:27:44 GMT

 XXXX@XXXXX.COM  schrieb:
That means, the compiler doesnt know which class Date you want to use, 
because you imported java.sql.Date and java.util.Date. Perhaps you did 
import java.sql.*; and import java.util.Date. or import java.util.*;
So do not import both. Import only one, so that the compiler knows which 
one to use.
You also can specifiy it explicitly with: out.write(""+new 
bye, Ralf

Re: Writing date and time in a log file in java

Postby IchBin » Tue, 05 Sep 2006 03:38:44 GMT

You could look at or use the code below. You can just pass the message 
to the function. The three functions you can use are:

1 - Write messages to console only
     setLogToConsole(String inTarget)
2 - Write messages to log file only
     setLogToFile(String inTarget)
3 - Write messages to console and log file
     setLog(String inTarget)

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class JAppLog {
public String getLogDate() {
     Object formatter = new SimpleDateFormat("yyMMddkk:mm:ss:SSS(z)");
     return (((DateFormat) formatter).format(new Date()).toString() + ": ");
public String getLogFileName() {
     Object formatter = new SimpleDateFormat("yyMMdd");
     return (((DateFormat) formatter).format(new Date()).toString() + 
public void setLog(String inTarget)
public void setLogToConsole(String inTarget) {
public void setLogToFile(String inTarget) {
     try {
         BufferedWriter out =
         new BufferedWriter(new FileWriter(getLogFileName(),
     } catch (IOException e) {

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA               http://www.**--****.com/ 
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)

Similar Threads:

1.Date & Time chooser for java 1.1 - using only the mouse to select time & date

2.Logging exception with java.util.logging.Logger#log() to file only

I'm logging exceptions with  java.util.logging.Logger#log() (i can't
use anything apart from standard Java API) and i don't want any stack
trace to appear in stdout or stderr is there any way I can filter or
redirect all output that is produced buy this method into the file

3.Java Logging files getting over-written despite settings.


I have set up Java 1.4 logging and added to my logging config file the

java.util.logging.FileHandler.append = TRUE

This doesn't appear to work though as each time I run the program I
get a new file. I have looked at the source-code and there is no case-
sensitivity on the "true".

Here's the contents of my config file, can someone please tell me if
I've missed something or if there's a good site that details these

handlers= java.util.logging.FileHandler,

java.util.logging.FileHandler.pattern = C:/logs/fims_archiver%g.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.level = INFO
java.util.logging.FileHandler.append = TRUE
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter =

java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter =


4.Writing date and time to a document.

Hi all,

I am a newbie to JavaScript and am just trying to get a script going that 
will write the ful date and time to each webpage as it is viewed.  Can 
anyone point out what mistakes there are in my code, as it does not seem to 
work?  I am actually trying to learn what parts of the code do what and why, 
so if anyone can take the time to explain it would be much appreciated. 
Here is my script:

<script language="javascript">

function datenTimes() {

var timeNow = new Date();

var day = timeNow.getDay();
var date = timeNow.getDate();
var month = timeNow.getMonth();
var minutes = timeNow.getMinutes();
var hours = timeNow.getHours();
var seconds = timeNow.getSeconds();

document.write(day + ", ");
document.write(month + ", ");
document.write(minutes + ":");
document.write(hours + ":");
document.write(seconds + ".");
document.write("This page was last updated:" + document.lastModified);




5.Date/Time Missing in Zip files created by java.util.zip

6. Writing to a file log from different threads

7. [java.util.logging] logging only to _one_ file

8. log java.util.logging.Logger to a file

Return to JAVA


Who is online

Users browsing this forum: No registered users and 41 guest