I have to attach a timestamp to a string with optional date format. (
to create a file )
Eg:
String str = new("filenameMM-DD-YYYY.txt");
or
String str = new("filename.txt");
I need something like str.format(date) which would replace the date
format (if present ) with the current timestamp.
Any help ?
Thanks
Sandeep
opalpa@gmail.com opalinski from opalpaweb - 22 Jan 2006 13:22 GMT
package pawel;
import java.util.*;
import java.text.*;
public final class TimeStamp {
private String str;
final static private SimpleDateFormat format
= new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
public TimeStamp() {
str = format.format(new java.util.Date());
}
public String toString() {
return str;
}
public boolean equals(Object o) {
return o instanceof TimeStamp && str.equals(""+o);
}
public int hashCode() {
return str.hashCode();
}
public static void main(String args[]) {
System.out.println(""+new TimeStamp());
}
}
Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
Sandeep - 22 Jan 2006 14:01 GMT
My Problem is slightly different .
I have a String which _may_ have a time format specified within
itself... something like...
Date d = new Date();
String str = "fileMM-DD-YY.txt";
d.format(str);
which would replace the "time format" in str with the value of object
"d".
> package pawel;
> import java.util.*;
[quoted text clipped - 23 lines]
> opalpa@gmail.com
> http://www.geocities.com/opalpaweb/
Rhino - 22 Jan 2006 13:35 GMT
>I have to attach a timestamp to a string with optional date format. (
> to create a file )
[quoted text clipped - 8 lines]
>
> Any help ?
I don't think it is correct to refer to something that contains only a year,
month, and day as a timestamp; I believe that a timestamp also needs to
contain hours, minutes, seconds, and fractional seconds (sometimes
milliseconds, sometimes microseconds, sometimes nanoseconds). However,
perhaps that is nitpicking.
The following code will determine the current month, day, and year for you:
GregorianCalendar now = new GregorianCalendar();
int intCurrentMonth = now.get(GregorianCalendar.MONTH) + 1;
int intCurrentDay = now.get(GregorianCalendar.DAY_OF_MONTH);
int intCurrentYear = now.get(GregorianCalendar.YEAR);
Remember that months are 0-based in Java, i.e. January is month 0, so I've
added 1 to the month so that January is 1, February is 2, etc.
Now, all you need to do is write some code to:
- convert the ints to Strings
- prepend zeroes to the values of the month and day - after all, you don't
want 1-1-2006 for January 1 2006, you want 2006-01-01
- concatenate the padded values together into a String with your filename.
For instance, something like this:
String currentMonth = Integer.toString(intCurrentMonth);
if (intCurrentMonth <= 9) currentMonth = "0"+currentMonth;
String currentDay = Integer.toString(intCurrentDay);
if (intCurrentDay <= 9) currentDay = "0"+currentDay;
String currentYear = Integer.toString(intCurrentYear);
String currentDate = currentMonth + "-" + currentDay + "-" currentYear;
String fullFileName = "filename"+currentDate+".txt";
Rhino
Tom Leylan - 22 Jan 2006 15:49 GMT
"Sandeep" <sandeepsinghal@gmail.com> wrote...
>I have to attach a timestamp to a string with optional date format. (
> to create a file )
[quoted text clipped - 6 lines]
> I need something like str.format(date) which would replace the date
> format (if present ) with the current timestamp.
People seem to be interpreting your question as "how do create and
concatenate a date string" which I think we can see it isn't. Your main
problem (it seems to me) is to recognize whether there is a date on the
filename presently. You have to derive a function to test for the presence
of a date which doesn't mistake some legitimate filename for your
concatenated string.
It isn't hard to do but you should isolate it into a single method
IsDatePresent() or something like that so you can adjust the code if you get
a false positive at some point. You know it should be attached to the end
of the filename, it should follow a 99-99-9999 convention and the filename
cannot be shorter than 11 characters or there couldn't be a date string
attached (it had to have at least one character initially).
If the filename passed was "test00-00-1234" you can see this might get by as
it coincidentally matches. In that case you could improve IsDatePresent(),
add a test for valid dates and this example would report False.
Tom
Sandeep - 22 Jan 2006 16:19 GMT
>Tom Leylan wrote:
> People seem to be interpreting your question as "how do create and
[quoted text clipped - 10 lines]
> cannot be shorter than 11 characters or there couldn't be a date string
> attached (it had to have at least one character initially).
I Think you are the closest to my problem. To add to what I already
wrote, I want to give an option of specifying the timestamp format to
the user and append the timestamp to it if any formatting is present.
So the format can be "MM-DD-YYYY" or "MM-DD-YYYY 0.0.0" or any format
that is supported by "DateFormat" class. Looks like there is no direct
function which can do this - and writing a custom function to check for
all such cases would be very difficult ... I probably would restrict
the number of formats that can be specified.
Sandeep
Ian Mills - 22 Jan 2006 17:01 GMT
>>Tom Leylan wrote:
>>People seem to be interpreting your question as "how do create and
[quoted text clipped - 21 lines]
>
> Sandeep
How do you intend to capture the format required? Is it going to be a
free-hand entry or a series of options on the screen with different
pre-defined formats available?
Sandeep - 22 Jan 2006 17:16 GMT
> How do you intend to capture the format required? Is it going to be a
> free-hand entry or a series of options on the screen with different
> pre-defined formats available?
Through an XML :- With any invalid string not converted to a timestamp
- any reconizable format to be replaced by a timestamp.
Ian Mills - 22 Jan 2006 17:38 GMT
>>How do you intend to capture the format required? Is it going to be a
>>free-hand entry or a series of options on the screen with different
>>pre-defined formats available?
>
> Through an XML :- With any invalid string not converted to a timestamp
> - any reconizable format to be replaced by a timestamp.
The simplest way it would seem to me would be to take the string entered
in the xml file for the format and create a new SimpleDateFormat, use
this to format the date and then append this to your file name.
e.g.
try
{
SimpleDateFormat dateFormat = new SimpleDateFormat("format from file");
}
catch (Exception e)
{
//format is invalid so process error
}
String fileName = "filename" + dateFormat.format(new Date());
hope this is something along the lines of what you were asking
Sandeep - 23 Jan 2006 02:17 GMT
I Think this would be the cleanest solution to the problem I was trying
to solve.
Thanks.
Sandeep
> The simplest way it would seem to me would be to take the string entered
> in the xml file for the format and create a new SimpleDateFormat, use
[quoted text clipped - 13 lines]
>
> hope this is something along the lines of what you were asking
P.Hill - 25 Jan 2006 08:24 GMT
> I Think this would be the cleanest solution to the problem I was trying
> to solve.
Then figure out what character means quote the next character(s) exactly
and make the user put that in an XML file instead of parsing something
like it yourself. Use the SimpleDateFormat with that better format
specification and you've got the problem solved.
Sounds like all you need is to read the Java doc's and your have it
in a few seconds.
-Paul
Roedy Green - 22 Jan 2006 22:58 GMT
>I need something like str.format(date) which would replace the date
>format (if present ) with the current timestamp.
see http://mindprod.com/jgloss/file.html
File.setLastModified

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 23 Jan 2006 01:14 GMT
>String str = new("filenameMM-DD-YYYY.txt");
>or
>String str = new("filename.txt");
>
>I need something like str.format(date) which would replace the date
>format (if present ) with the current timestamp.
It is always easier to explain these problems with a few examples.
Is this what you mean?
rabbit.txt -> rabbit01-22-2006.txt
rabbit12-31-2005 -> rabbit01-22-2006.txt
First I would use iso date format YYYY-MM-DD , rather than MM-DD-YYYY
so there is no ambiguity about DD-MM or MM-DD. It also sorts in order
properly in directory listings if you have dups. You migh consider
putting the date on the front, depending on your sorting needs.
You have several problems:
1. deterimine if there is a date on the end already. You could do that
with a regex or just by testing each char in turn to see if 0-9 or -.
as appropriate for the slot. If there is one, either remove it an
replace, or leave it alone. If there isn't, add one.
see http://mindprod.com/jgloss/regex.html
2. generate an iso Date string with today's date.
See http://mindprod.com/jgloss/calendar.html#TODAY
for the code.
3. You may need to rename existing files. Use File.rename
See http://mindprod.com/jgloss/file.html
4. you may need to redate (touch) existing files. Use
File.setLastModified ( System.currentTimeInMillis())
See http://mindprod.com/jgloss/file.html
http://mindprod.com/jgloss/time.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.