Home of www.RoboHobby.com Java robotics project www.CubesAndCrosses.com
Train your logic skills in geometry
Create and delete one-color crosses
Play for free right in your browser!
Home-made CNC machine
Our new project -
How to make home-made CNC router
PICkit2 - Tutorials, Experiments
PICkit 2 - Small size USB programmer
from Microchip.
How to use it as a demo device for
USB programming in C language




www.RoboHobby.com
Home of www.RoboHobby.com
Java robotics project


Portable Notebook Table
How to make Portable Notebook Table.
Do it yourself!

Java SE SWING JSR-296 Example
How to run background task in
JSR-296 SWING application
with 'busy animation' in the status bar.
Useful addition to Netbeans template.

Java ME Code Examples
Java ME Source code examples
for robotics projects


Run Java ME Code Examples
How to run our Java ME code examples
for robotics projects



www.RoboHobby.com
Once upon a time, long-long ago, when skys were much more blue then today,
girls were much more beautiful and Java was just young programming language for applets,
I created this class - StrCalendar for String-to-Calendar and Calendar-to-String conversion.
When it was? Hmm... it was long ago, may be in a previous millennium, when there were no
good String-Calendar converters in standard SUN Java.

I thought it's over - now days it is not the time of such a 'hand-made' converters.
But I was wrong. Soon I found that even now we need this simple class.
We can use it in J2ME. It is appear that history make it's cycle and there is a need
of good-old StrCalendar for String-Calendar transformation - now in Java Microedition.

May be in the nearest future we will see next version of new kind of 'small' Java
(may be embedde Java), where again we can use this class...


package com.robohobby.me;
import java.util.Calendar;

/**
 * J2ME code example. Java ME Disc Utils
 * Part of www.RoboHobby.com Java robotics project.
 * www.RoboHobby.com

 * Utility class for String-to-Calendar and

 * Calendar-to-String conversion.
 * Usefull for some Java ME versions, especially for Java ME.

 * @Ads.Taglet
 */
public class StrCalendar {

    /**
     * Method for conversion from Java String type to Calendar type.

     * Input string should be in 'Java Date/time format'.

     * It should look, like this: 2009-10-11 11:45:59
     * Year-Month-Hour HH:MM:SS

     * @param str
     * @return
     */
    static public java.util.Calendar strToCalendar(String str) {
        //For strings, like: 2003-03-18 15:46:53

        int y, month, date, h, min, sec;

        y = Integer.parseInt(str.substring(0, 4));
        month = Integer.parseInt(str.substring(5, 7));
        month -= 1;
        date = Integer.parseInt(str.substring(8, 10));
        h = Integer.parseInt(str.substring(11, 13));
        min = Integer.parseInt(str.substring(14, 16));
        sec = Integer.parseInt(str.substring(17, 19));

        java.util.Calendar c = java.util.Calendar.getInstance();
        //c.clear();
        //c.set(y,month,date,h,min,sec);
        c.set(Calendar.YEAR, y);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DATE, date);
        c.set(Calendar.HOUR_OF_DAY, h);
        c.set(Calendar.MINUTE, min);
        c.set(Calendar.SECOND, sec);

        return c;

    }

    /**

     * Method for converstion from Java Calendar to Java type String.

     * Returns JavaString in 'Java Date/Time format'.

     *
     * @param c
     * @return
     */
    static public String calendarToStr(java.util.Calendar c) {
        int y = c.get(java.util.Calendar.YEAR);

        int m = c.get(java.util.Calendar.MONTH);

        m += 1;

        String mStr = addZero(m);

        int d = c.get(java.util.Calendar.DAY_OF_MONTH);
        String dStr = addZero(d);

        int h = c.get(java.util.Calendar.HOUR_OF_DAY);
        String hStr = addZero(h);

        int mn = c.get(java.util.Calendar.MINUTE);
        String mnStr = addZero(mn);

        int s = c.get(java.util.Calendar.SECOND);
        String sStr = addZero(s);

        return ("" + y + "-" + mStr + "-" + dStr + " " + hStr + ":" + mnStr + ":" + sStr);
    }

    /**

     * Subversion of method calendartoStr,
     * safe  for file system.

     * Using this method you can get string without symbols ":" and 'spacebar'.

     * As a result you can use this string as a file name for saving some info

     * in  file on a disc, etc.
     * Resulting strings should look like: 2009-10-11_11-45-59 and this format is

     * 'file-system friendly'.
     *
     * @param c

     * @return Date in Java format
     */
    static public String calendarToStrFs(java.util.Calendar c) {
        int y = c.get(java.util.Calendar.YEAR);

        int m = c.get(java.util.Calendar.MONTH);

        m += 1;

        String mStr = addZero(m);

        int d = c.get(java.util.Calendar.DAY_OF_MONTH);
        String dStr = addZero(d);

        int h = c.get(java.util.Calendar.HOUR_OF_DAY);
        String hStr = addZero(h);

        int mn = c.get(java.util.Calendar.MINUTE);
        String mnStr = addZero(mn);

        int s = c.get(java.util.Calendar.SECOND);
        String sStr = addZero(s);

        return ("" + y + "-" + mStr + "-" + dStr + "_" + hStr + "-" + mnStr + "-" + sStr);
    }

    private static String addZero(int d) {
        String dStr;
        if (d < 10) {
            dStr = "0" + d;
        } else {
            dStr = "" + d;
        }
        return dStr;
    }
}










Google
 



Contact us:


My Skype name is oleg_skp
Start Skype text chatStart Skype text chat
View Skype profileView Skype profile
Start Skype voice callStart Skype voice call
Add me to your Skype contact listAdd to Skype contact list
Leave voicemail using SkypeLeave voicemail using Skype
Send a file using SkypeSend a file using Skype
Note: To use these links
the Skype program should
be installed on your computer.
You can load it for free from
their official site:
Contact via Skypewww.Skype.com
------------------