Categories
discuss

Moment JS start and end of given month

I need to calculate a JS date given year=2014 and month=9 (September 2014).

I tried this:

var moment = require('moment');
var startDate = moment( year+'-'+month+'-'+01 + ' 00:00:00' );
            var endDate = startDate.endOf('month');
            console.log(startDate.toDate());
            console.log(endDate.toDate());

Both of logs show:

Tue Sep 30 2014 23:59:59 GMT+0200 (CEST)
Tue Sep 30 2014 23:59:59 GMT+0200 (CEST)

End date is correct but… why the start date is not?

Answer

That’s because endOf mutates the original value.

Relevant quote:

Mutates the original moment by setting it to the end of a unit of time.

Here’s an example function that gives you the output you want:

function getMonthDateRange(year, month) {
    var moment = require('moment');

    // month in moment is 0 based, so 9 is actually october, subtract 1 to compensate
    // array is 'year', 'month', 'day', etc
    var startDate = moment([year, month - 1]);

    // Clone the value before .endOf()
    var endDate = moment(startDate).endOf('month');

    // just for demonstration:
    console.log(startDate.toDate());
    console.log(endDate.toDate());

    // make sure to call toDate() for plain JavaScript date type
    return { start: startDate, end: endDate };
}

References:

Categories
discuss

Java – Odd memory consumption between x32 and x64

I’ve been profiling the x64 version of my application as the memory usage has been outrageously high, all of it seems to be coming from the JavaFX MediaPlayer, i’m correctly releasing listeners and eventhandlers.

Here is the stark contrast.

The x32 version at start

enter image description here

And now the x64 version at start

enter image description here

The x32 version stays below 256mb while the x64 will shoot over a gig; this is while both are left to play through their playlist.

All the code is the same.

JDK: jdk1.8.0_20

JRE: jre1.8.0_20

VM arguments on both

-XX:MinHeapFreeRatio=40 -XX:MaxHeapFreeRatio=70 -Xms3670k -Xmx256m -Dsun.java2d.noddraw=true -XX:+UseParallelGC

Same issue occurring on another x64 Java application

enter image description here

Is this a bug or am I overlooking something?

Answer

What you are seeing is the memory usage of the entire JVM running your process. The -Xmx256m setting only limits the maximum heap space available for your application to allocate (and the JVM would enforce that). Outside of heap space, the JVM can use additional memory for a host of other purposes (I am sure I will miss a few in the list below):

  • PermGen, which has now be replaced by the Metaspace. According to the documentation, there is no default limit for this:

    -XX:MaxMetaspaceSize=size
    Sets the maximum amount of native memory that can be allocated for class metadata. By default, the size is not limited. The amount of metadata for an application depends on the application itself, other running applications, and the amount of memory available on the system.
    
  • Stack space (memory used = (number of threads) * stack size. You can control this with the -Xss parameter

  • Off-heap space (either use of ByteBuffers in your code, or use of third pary libraries like EHCache which would in turn use off-heap memory)

  • JNI code

  • GC (garbage collectors need their own memory, which is again not part of the heap and can vary greatly depending on the collector used and the application memory usage)

In your case, you are seeing the “almost doubling” of memory use, plus probably a more relax Metaspace allocation when you move from a 32bit to a 64bit JVM. Using -XX:MaxMetaspaceSize=128m will probably bring the memory usage down to under 512MB for the 64bit JVM.

Categories
discuss

How to wrap text of one TextView under other Textviews in android?

does anyone know how it is possible to wrap text of one TextView under another TextViews, when all they are located in one line one after another?

Here is an example of what I have and what i need (I can’t insert image here):
http://www.imagesup.net/?di=1014121123675

Answer

You need to use one TextView for all the text. If you need them to have different styles, look into using SpannableStringBuilder to build the string and add spans to affect the styles.

Categories
discuss

A generics confusion: deceiving the compiler

Consider a code:

public class GenericsConfusion {
    public static <T> Class<T> get(Class<T> clazz) {
        Map<Class, Class> map = new HashMap<Class, Class>();
        map.put(Integer.class, String.class);
        return map.get(clazz);
    }

    public static void main(String[] args) {
        Class<Integer> clazz = get(Integer.class);
        System.out.println(clazz);
    }
}

It compiles and runs perfectly. The idea was to return in get method the class which has the same type parameter as input class. But it is broken due to the presence of map. Yes, I know that in runtime the type parameter information is erased, so without the type parameters this code is perfectly valid. Also I know I can fix it by specifying Map<Class<T>, Class<T>> But the fact is that in method signature I have type parameters, and they dont help me in compile time.

Is this a misuse of some concept?

Or it is a disadvantage of Java generics?

Or its perfectly ok and I misunderstand the idea of type parameters?

Answer

A raw type, such as Class or Map (as opposed to Class<...> or Map<..., ...>), circumvents the type-checking of generics. You can even write something like this:

final Class<Integer> whoops = (Class) String.class;

This is an unfortunate weakness in the type system. It was originally included in Java 5 (which introduced generics) for compatibility with code written under previous versions.

For the most part, you can avoid this weakness by avoiding the use of raw types. Your compiler should warn you about them.

Unfortunately, there are various circumstances where raw types are essentially unavoidable (due to the special typing of .getClass(); due to the fact that we can only write (for example) Map.class and not Map<String, String>.class (or Map.<String, String>class); due to erasure and reflection, etc.); but happily, as you’ve noted, your circumstance does not appear to be one of these.

Categories
discuss

Minimum Google Play Services version for Push Notification

I am using Google Cloud Messaging to receive Push Notifications on Android phones. I have succeed and everything seems to work well. However, Google recommends to check if Google Play Services is updated to the last version (because Push Notifications is a new feature).

Using a Nexus 7 (4.4.4) and enabling version checking at the beginning of the Activity I am required to update my Google Play Services since there is a newer version. However, Push Notifications work perfectly without the update. I do not want to require my users to update Google Play Services if it is not absolutely necessary.

My question: what is the minimum version that I should check for Google Play Services in order to receive Push Notifications from GCM?

This is the code:

    private boolean checkPlayServices() {
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
                GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                        PLAY_SERVICES_RESOLUTION_REQUEST).show();
            } else {
                Log.i(TAG, "This device is not supported.");
                finish();
            }
            return false;
        }
        return true;
    }

Remark: I don’t need to check for the latest version of Google Play Services but the minimum version supporting Push Notifications

EDIT: I’ve made some clarification due to comments

Answer

I have an application with GCM enabled. As I saw in my database, there exists users with Google Play Sercvices version more than 3.0.27.

But be carefull about using GcmNetworkManager, as it needs at least google play service 7.5.0

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..