Sunday, 30 June 2013

How to Set TimeStamp from a Date object and change it in #Java ? #JDBC


You can find the current Date using :

import java.sql.Timestamp;
import java.util.Date;
 
public class GetCurrentTimeStamp 
{
    public static void main( String[] args )
    {
  java.util.Date date= new java.util.Date();
  System.out.println(new Timestamp(date.getTime()));
    }
}


you can also set a special date by using below codes.but notice the trike in the month field 

Date februaryTheFirst = new Date(2013,1,1); // equals 2012-02-01
This might explain what you are seeing. If you want to instantiate 2012-01-01 instead, you should do:
Date firstDayOf2012 = new Date(2012,0,1); // this is 2012-01-01
Exactly the same thing happens when dealing with Calendar:
Calendar.getInstance().set(2012,0,1); // 2012-01-01

If you want to change in Days ..

Calendar cal = Calendar.getInstance();
cal.setTime(dateInstance);
cal.add(Calendar.DATE, -30);
Date dateBefore30Days = cal.getTime();

also u can do it using Date Object:

Date d = new Date();//intialize your date to any date Date dateBefore = new Date(d.getTime() - n * 24 * 3600 * 1000 ); //Subtract n days 

Friday, 28 June 2013

Add pictures in Jframe - #Netbeans #java

in this tutorial you can add a picture to JFrame in 5 steps.

  1. add text Editor to Jframe.
  2. right click --> properties.
  3. Click Icon --> Import to project.
  4. then click Ok.
  5. Click Edit text -- remove generated Text.

Using the code , you can do:

  1. jLabel1 = new javax.swing.JLabel();
  2. String icon_xpath = "jLabel4.icon";
  3. jLabel1.setIcon(icon_xpath); // NOI18N
  4. jform.Add(jLabel1);

Monday, 24 June 2013

OutOfMemoryErrors - java


Some JVMs (Java virtual  machine) put restrictions on the total amount of memory available on the heap. 

If you are getting OutOfMemoryErrors while running Eclipse, the VM can be told to let the heap grow to a larger amount by passing the -vmargs command to the Eclipse launcher. 
eclipse [normal arguments] -vmargs -Xmx256M [more VM args]

You can put the extra memory while initialising the Eclipse by opening  the file  eclipse.ini.
and change the value of Xms512m
Here is an example;

-startup
plugins/org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.gtk.linux.x86_64_1.1.200.v20120913-144807
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
--launcher.defaultAction
openFile
-vmargs
-Xms512m

-Xmx1024m

-XX:+UseParallelGC -XX:PermSize=256M -XX:MaxPermSize=512M



  • If the problem is while running a special Test-Case or  Running the project you can increase the reserved memory from JVM by a memory by:

Right click --> Run As --> Run Configurations then click on second tap (x= arguments)
and add -Xmx512M in VM arguments,
               

Best Of luck.

Sunday, 23 June 2013

Add Swing plugin to Eclipse.

To add the swing plugin for easy buildup your Desktop GUI using Eclipse


 Just follow these tips:

  1. Open your "Eclipse " .
  2. Go to "Help" tab.
  3. Click in "Install new Software..."
  4. A new window will appear, in the field "type or select a site" put: "http://dl.google.com/eclipse/inst/d2wbpro/latest/4.2" and press "Enter".
  5. Select all available updates and click "Next".
  6. Now just proceed the installation. Later you will need restart the Eclipse. Do that.
enjoy :)

Wednesday, 19 June 2013

how to make an Activity a main luncher ?

go to AndroidManifest.xml  in your project and show it in xml view

search for target activity to be a main luncher and add the following tag

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

for example:


 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.LoginActivity"
            android:label="@string/app_name"
             <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

Tuesday, 18 June 2013

Unsupported major.minor version 50.0 - JAVA

java.lang.UnsupportedClassVersionError:  (Unsupported major.minor version 50.0)


java.lang.unsupportedclassversionerror unsupported major.minor version 50.0 error comes in Java environment 

when you compile your Java file in a higher Java version e.g. Java 1.6 and then trying to run same Java Code in lower version e.g. Java 1.5. Java is backward compatible 


Every Java release has a major source version as shown below table

Java Version    Major Version
Java 4          48.0
Java 5          49.0
java 6          50.0
java 7          51.0

How TO Solve it ?


to solve it you have to  run the code correct Java version. To adapt it you can try

1) change the JAVA_HOME & path in environment variables ( XP -  win7) and check it.
to check it you can run in console "java -version"

2) IF you are using Eclipse IDE . you can check the compilation version by Windows --> preferences --> java --> compiler  




References: