You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
3.0 KiB
107 lines
3.0 KiB
14 years ago
|
// YMarkDate.java
|
||
|
// (C) 2011 by Stefan Förster, sof@gmx.de, Norderstedt, Germany
|
||
|
// first published 2010 on http://yacy.net
|
||
|
//
|
||
|
// This is a part of YaCy, a peer-to-peer based web search engine
|
||
|
//
|
||
|
// $LastChangedDate: 2011-03-09 13:50:39 +0100 (Mi, 09 Mrz 2011) $
|
||
|
// $LastChangedRevision: 7574 $
|
||
|
// $LastChangedBy: apfelmaennchen $
|
||
|
//
|
||
|
// LICENSE
|
||
13 years ago
|
//
|
||
14 years ago
|
// This program is free software; you can redistribute it and/or modify
|
||
|
// it under the terms of the GNU General Public License as published by
|
||
|
// the Free Software Foundation; either version 2 of the License, or
|
||
|
// (at your option) any later version.
|
||
|
//
|
||
|
// This program is distributed in the hope that it will be useful,
|
||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
// GNU General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU General Public License
|
||
|
// along with this program; if not, write to the Free Software
|
||
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||
|
|
||
12 years ago
|
package net.yacy.data.ymark;
|
||
14 years ago
|
|
||
|
import java.text.ParseException;
|
||
|
import java.text.SimpleDateFormat;
|
||
|
import java.util.Date;
|
||
|
|
||
|
import net.yacy.cora.date.ISO8601Formatter;
|
||
|
import net.yacy.cora.document.UTF8;
|
||
|
|
||
|
public class YMarkDate {
|
||
13 years ago
|
|
||
14 years ago
|
private long date;
|
||
13 years ago
|
|
||
14 years ago
|
public YMarkDate() {
|
||
13 years ago
|
this.date = System.currentTimeMillis();
|
||
14 years ago
|
}
|
||
13 years ago
|
|
||
14 years ago
|
public YMarkDate(final byte[] date) {
|
||
|
this.set(date);
|
||
|
}
|
||
13 years ago
|
|
||
13 years ago
|
public YMarkDate(final Date date) {
|
||
|
this.date = date.getTime();
|
||
|
}
|
||
13 years ago
|
|
||
14 years ago
|
public long parseISO8601(final String s) throws ParseException {
|
||
|
if(s == null || s.length() < 1) {
|
||
|
throw new ParseException("parseISO8601 - empty string, nothing to parse", 0);
|
||
|
}
|
||
|
SimpleDateFormat dateformat;
|
||
|
StringBuilder date = new StringBuilder(s);
|
||
|
if(s.length()==10)
|
||
|
dateformat = new SimpleDateFormat("yyyy-MM-dd");
|
||
|
else {
|
||
13 years ago
|
dateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
|
||
14 years ago
|
if(date.charAt(date.length()-1) == 'Z') {
|
||
|
date.deleteCharAt(date.length()-1);
|
||
|
date.append("GMT-00:00");
|
||
|
} else {
|
||
|
date.insert(date.length()-6, "GMT");
|
||
|
}
|
||
|
}
|
||
|
this.date = dateformat.parse(date.toString()).getTime();
|
||
|
return this.date;
|
||
|
}
|
||
13 years ago
|
|
||
14 years ago
|
public String toISO8601() {
|
||
13 years ago
|
return (this.date == 0) ? YMarkEntry.BOOKMARK.DATE_MODIFIED.deflt() : ISO8601(new Date(this.date));
|
||
14 years ago
|
}
|
||
13 years ago
|
|
||
13 years ago
|
public static String ISO8601(final Date date) {
|
||
13 years ago
|
return ISO8601Formatter.FORMATTER.format(date);
|
||
13 years ago
|
}
|
||
13 years ago
|
|
||
14 years ago
|
public byte[] toBytes() {
|
||
|
return String.valueOf(this.date).getBytes();
|
||
|
}
|
||
13 years ago
|
|
||
|
@Override
|
||
14 years ago
|
public String toString() {
|
||
|
return String.valueOf(this.date);
|
||
|
}
|
||
13 years ago
|
|
||
14 years ago
|
public long get() {
|
||
|
return this.date;
|
||
|
}
|
||
13 years ago
|
|
||
14 years ago
|
public void set(long date) {
|
||
|
this.date = date;
|
||
|
}
|
||
13 years ago
|
|
||
14 years ago
|
public void set(byte[] date) {
|
||
14 years ago
|
final String s = UTF8.String(date);
|
||
|
if(!s.isEmpty()) {
|
||
13 years ago
|
this.date = Long.parseLong(s);
|
||
14 years ago
|
} else {
|
||
13 years ago
|
this.date = 0;
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|
||
|
}
|