When converting string date representation to numeric values, I obtain a different result in Java/Groovy/PHP vs in Javascript. For some dates before 1970, the JS timestamp is exactly 3600 secs before the Java timestamp. I could reproduce it for Oct 1st, but for Jan 1st it's ok.
My test case (in groovy, using the usual Java API on purpose):
def sdf = new SimpleDateFormat("dd/MM/yyyy")
["01/10/1956", "01/01/1956", "01/10/1978"].each {
def d = sdf.parse(it)
println "${it} -> ${d.time}"
}
and in JS (I simply run it from the Chrome console - "9" is October here):
new Date(1956, 9, 1, 0, 0, 0).getTime()
A few samples:
*Groovy
01/10/1956 -> -418179600000
01/01/1956 -> -441853200000
01/10/1978 -> 276040800000
*Javascript
1956,9,1,0,0,0 -> -418183200000
1956,0,1,0,0,0 -> -441853200000
1978,9,1,0,0,0 -> 276040800000
=> Notice how 01/10/1956 is not converted the same way, yielding a 3600 seconds difference.
A daylight saving time or a timezone would be the perfect culprit but I don't see why the two universe diverged at some point in the past.
Any hint welcome!
Thank you
EDIT more samples
*Java/Groovy
01/01/1974 -> 126226800000
01/10/1974 -> 149814000000
01/01/1976 -> 189298800000
01/10/1976 -> 212972400000
01/01/1978 -> 252457200000
01/10/1978 -> 276040800000
*JS
new Date(1974, 0, 1, 0, 0, 0).getTime() 126226800000
new Date(1974, 9, 1, 0, 0, 0).getTime() 149814000000
new Date(1976, 0, 1, 0, 0, 0).getTime() 189298800000
new Date(1976, 9, 1, 0, 0, 0).getTime() 212972400000
new Date(1978, 0, 1, 0, 0, 0).getTime() 252457200000
new Date(1978, 9, 1, 0, 0, 0).getTime() 276040800000
Around 1967~1971
01/01/1967 -> -94698000000
01/04/1967 -> -86922000000
01/10/1967 -> -71110800000
01/01/1968 -> -63162000000
01/04/1968 -> -55299600000
01/10/1968 -> -39488400000
01/01/1971 -> 31532400000
01/10/1971 -> 55119600000
new Date(1967, 0, 1, 0, 0, 0).getTime() -94698000000
new Date(1967, 3, 1, 0, 0, 0).getTime() -86925600000
new Date(1967, 9, 1, 0, 0, 0).getTime() -71114400000
new Date(1968, 0, 1, 0, 0, 0).getTime() -63162000000
new Date(1968, 3, 1, 0, 0, 0).getTime() -55303200000
new Date(1968, 9, 1, 0, 0, 0).getTime() -39492000000
new Date(1971, 0, 1, 0, 0, 0).getTime() 31532400000
new Date(1971, 9, 1, 0, 0, 0).getTime() 55119600000
Your profile says you're from Belgium.
There's no daylight saving time in 1976 for Brussels:
http://www.timeanddate.com/worldclock/clockchange.html?n=48&year=1976
But there is from 1977 onwards:
http://www.timeanddate.com/worldclock/clockchange.html?n=48&year=1977
Java is probably aware of this, whereas JavaScript is not.
The information about Timezones is complex and you would be surprised how often a) they change, b) they are inaccurate.
I would try this in Java/Groovy as well.
new Date(1956, 9, 1, 0, 0, 0).getTime()
A cool website on timezones. http://www.bbc.co.uk/news/world-12849630
For example, the epoch is 1970/01/01 00:00 UTC. Not Europe/London because even though it was winter, the UK was in BST (British Summer Time) This only happen from Feb 1968 to Nov 1971. :P http://www.timeanddate.com/time/uk/time-zone-background.html
The more you learn about time and date the more you realise its all rather adhoc. Even UTC is not an acronym as such, it means "Coordinated Universal Time" in English and "Temps Universel Coordonné" in French, and because they couldn't agree on what the acronym should be the compromise was UTC with is neither. http://en.wikipedia.org/wiki/Coordinated_Universal_Time
Related
I'm passing an attribute "hours" containing the following values:
ArrayList<Integer> hours = new ArrayList<>(Arrays.asList({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23}));
model.addAttribute("hours", hours);
model.addAttribute("dto", dto);
on my jsp:
...
<form:select id="to" path="to" class="form-control chosen-select"
items="${hours}" value="${dto.to}" />
...
I have tried outputting the values on my jsp and they're all correct, the only problem is that they're completely wrong. dto.to should be an Integer "3" but it shows 13 as default selected value. What's the problem here??
Is it possible to find out the comment levels from this web like below?
https://www.ozbargain.com.au/node/249439#comment-3719026
From jsoup I am able to parse the comments, username etc, but I am having trouble getting the correct comment levels.
Viewing the source of that page, the doesn't match with the correct live posts, unless I am reading it all wrong.
Is there a way to solve this?
I was able to generate the source comment level using:
String url = "https://www.ozbargain.com.au/node/249439";
Document doc = Jsoup.connect(url).get();
Elements level = doc.select("ul.comment");
for(Element column : e.select("ul")){
//comment level
System.out.println(column.attr("class"));
levels.add(column.attr("class"));
}
But its doesn't look right. Only showing 1 of level 0 comment etc.
Thanks
for(Element column : e.select("ul")) {
//comment level
System.out.println(column.attr("class"));
levels.add(column.attr("class"));
}
From the above code where does the e comes from?
Anyway, you need to parse the class attribute value in order to find the comment level.
Here is a working sample code:
SAMPLE CODE
public static void main(String[] args) throws IOException {
String url="https://www.ozbargain.com.au/node/249439#comment-3719026";
Document doc = Jsoup.connect(url).get();
Elements comments = doc.select("div.comment-wrap");
Matcher levelMatcher = Pattern.compile("(?i)^(.*level)(\\d+)(.*)$").matcher("");
List<String> levels = new ArrayList<>();
System.out.println("Comments found: "+ comments.size());
for (Element comment : comments) {
if (levelMatcher.reset(comment.parent().parent().className()).find()) {
levels.add(levelMatcher.replaceAll("$2"));
}
}
System.out.println(levels);
}
OUTPUT [https://www.ozbargain.com.au/node/249439#comment-3719026] (may change depending on the request time)
Comments found: 38
[0, 1, 2, 3, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 2, 3, 3, 1, 2, 3, 3, 0, 1, 2, 3, 2, 3, 3, 2, 0, 0, 0, 1, 2, 3]
OUTPUT [https://www.ozbargain.com.au/node/249604] (may change depending on the request time)
Comments found: 14
[0, 1, 0, 1, 0, 1, 1, 2, 1, 0, 0, 1, 2, 0]
I have a JSON with Joda DateTime field. It has some sample values. But whenever I convert it to Object it automatically takes the current DateTime instead of the DateTime present in the JSON.
PFB the sample JSON
[{
"pas": "CSP",
"policyNumber": "ZU131874",
"schemeName": "PepsiCo employee scheme20",
"policyStatus": "ACTIVE",
"productCode": "GPP",
"totalSavings": 100000,
"investmentReturn": 55000,
"effectiveDate": {
"startDate": {
"dayOfYear": 2,
"year": 2014,
"dayOfMonth": 2,
"dayOfWeek": 4,
"era": 1,
"weekOfWeekyear": 1,
"millisOfSecond": 0,
"secondOfMinute": 0,
"minuteOfDay": 0,
"centuryOfEra": 20,
"yearOfCentury": 14,
"hourOfDay": 0,
"monthOfYear": 1,
"weekyear": 2014,
"minuteOfHour": 0,
"yearOfEra": 2014,
"secondOfDay": 0,
"millisOfDay": 0,
"millis": 1388601000000
},
"endDate": null
}
}, {
"pas": "CSP",
"policyNumber": "ZU146271",
"schemeName": "PepsiCo employee scheme7",
"policyStatus": "ACTIVE",
"productCode": "GPP",
"totalSavings": 100000,
"investmentReturn": 55000,
"effectiveDate": {
"startDate": {
"dayOfYear": 156,
"year": 2015,
"dayOfMonth": 5,
"dayOfWeek": 5,
"era": 1,
"weekOfWeekyear": 23,
"millisOfSecond": 0,
"secondOfMinute": 0,
"minuteOfDay": 0,
"centuryOfEra": 20,
"yearOfCentury": 15,
"hourOfDay": 0,
"monthOfYear": 6,
"weekyear": 2015,
"minuteOfHour": 0,
"yearOfEra": 2015,
"secondOfDay": 0,
"millisOfDay": 0,
"millis": 1433442600000
},
"endDate": null
}
}]
I am using following code to convert list of JSON objects to a list Of Java objects.
policies = new ArrayList<Policy>();
JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(new FileReader("./src/test/resources/" + "sample-zurich-pensions.json"));
Type listType = new TypeToken<List<Policy>>(){}.getType();
List<Policy> policyList = new Gson().fromJson(jsonElement, listType);
policies.addAll(policyList);
In the jsonElement I am getting the exact value, but in the policyList the DateTime is set to the current date.
PFB the classes
Policy.java
private String pas;
private String policyNumber;
private String schemeName;
private String policyStatus;
private String productCode;
private BigDecimal totalSavings;
private BigDecimal investmentReturn;
private EffectiveDate effectiveDate;
EffectiveDate.java
private DateTime startDate;
private DateTime endDate;
During deserialization from JSON, Gson is creating a new DateTime() (which is equal to current system DateTime). The fields present in your JSON are based on getters in DateTime, but there are no setters for them present, so the object cannot be adjusted to the timestamp represented by the JSON. You are much better off using a standard date-time representation like ISO 8601. Then, implement a JsonSerializer and JsonDeserializer for DateTime as suggested on the Gson site:
class DateTimeTypeConverter implements JsonSerializer<DateTime>, JsonDeserializer<DateTime> {
#Override
public JsonElement serialize(DateTime src, Type srcType, JsonSerializationContext context) {
return new JsonPrimitive(src.toString());
}
#Override
public DateTime deserialize(JsonElement json, Type type, JsonDeserializationContext context)
throws JsonParseException {
return new DateTime(json.getAsString());
}
}
or use one of the solutions provided in this post (linked also by #user2762451). You can register the serializer/deserializer like this:
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(DateTime.class, new DateTimeTypeConverter());
Gson gson = gsonBuilder.create();
I have a set of data in an array and would like to turn it into a list or added the data to a list. The stickyheaders library uses lists and I need to change mine into a list since I'm having difficulty in using the stickyheaders as arrays it only shows a blank recyclerview.
This is my set of sample data, this is data is just used for creating a new event in a calendar. I retrieve the day and format it to show (e.g Monday). This is what the header will be.
If anyone can help turning this array into a list or how to add these data to a list, it would be appreciated, or if someone is familiar with stickyheaders and used arrays for it some sample code is appreciated.
public static final Event[] EventList = {
new Event("The Theory of Everything", "1:00 PM", "Cinema 1", 2015, 2, 5, 13, 0),
new Event("Kingsman the Secret Service", "3:00 PM", "Cinema 2", 2015, 2, 5, 15, 0),
new Event("Fifty Shades of Grey", "5:00 PM", "Cinema 3", 2015, 2, 5, 17, 0),
new Event("Focus", "7:00 PM", "Cinema 4", 2015, 2, 5, 19, 0),
new Event("Crazy Beautiful You", "9:00 PM", "Cinema 5", 2015, 2, 5, 21, 0),
new Event("The Theory of Everything", "1:00 PM", "Cinema 1", 2015, 2, 6, 13, 0),
new Event("Kingsman the Secret Service", "3:00 PM", "Cinema 2", 2015, 2, 6, 15, 0),
new Event("Fifty Shades of Grey", "5:00 PM", "Cinema 3", 2015, 2, 6, 17, 0),
new Event("Focus", "7:00 PM", "Cinema 4", 2015, 2, 6, 19, 0),
new Event("Crazy Beautiful You", "9:00 PM", "Cinema 5", 2015, 2, 6, 21, 0)
};
You're almost there. After that you can do:
List<Event> eventList = Arrays.asList(EventList);
i need to create a classifier by feature, i have 15M rows of data like:
{
"app_entertainment" : 1,
"app_widgets" : 2,
"arcade" : 8,
"books_and_reference" : 2,
"comics" : 0,
"brain" : 20,
"business" : 0,
"cards" : 5,
"casual" : 1,
"communication" : 4,
"education" : 0,
"finance" : 1,
"game_wallpaper" : 0,
"game_widgets" : 0,
"health_fitness" : 0,
"libraries_demo" : 0,
"racing" : 1,
"lifestyle" : 1,
"media_video" : 0,
"medical" : 0,
"music_and_audio" : 7,
"news_magazines" : 2,
"personalization" : 1,
"photography" : 0,
"productivity" : 4,
"shopping" : 1,
"social" : 1,
"sports_apps" : 1,
"sports_games" : 7,
"tools" : 15,
"transportation" : 2,
"travel_and_local" : 8,
"weather" : 3,
"app_wallpaper" : 0,
"entertainment" : 0,
"health_and_fitness" : 0,
"libraries_and_demo" : 0,
"media_and_video" : 0,
"news_and_magazines" : 0,
"sports" : 0
}
also for every dataset like this i know if its true or false,
the boolean is if the user with this dataset clicked on ad or not.
how can i use mahout to train a classifier and how do i classify after i trained it?
everything that i found on the net is very abstract, not many examples of how to do it via java
There are very few materials for Mahout on the internet. I referred to the Mahout source code and the source code in Mahout in Action.
You could refer to 20newsgroup source code for classification.
A simple example using NavieBayes classifier. The vector is the dataset.
public List<String> classifyCase(Vector vector) {
TreeMap<Double, String> resultMap = new TreeMap<Double, String>();
Vector result = classifier.classifyFull(vector);
for (Vector.Element element: result) {
int categoryId = element.index();
double score = element.get();
resultMap.put(-score, labels.get(categoryId));
}
return new ArrayList<String>(resultMap.values());
}