...
// Test to change datetime items in text form
SimpleDateFormat sdfIn = new SimpleDateFormat( "dd.mm.yyyy" );
SimpleDateFormat sdfOut = new SimpleDateFormat( "yyyy.mm.dd" );
String strIn = "25.11.2023";
Date dt = sdfIn.parse( strIn );
String strOut = sdfOut.format( dt );
System.out.printf( "+++ Convert date format: '%s' -> '%s'%n", strIn, strOut );
...
Runtime rt = Runtime.getRuntime();
long usedMB = (rt.totalMemory() - rt.freeMemory()) / 1024 / 1024;
System.out.printf("Usage mamory %d Mb%n", usedMB);
/**
* Sets the current system look and feel properties
*/
public static void setSystemLookAndFeel() {
/* Get the default operation system style */
final String uiName = UIManager.getSystemLookAndFeelClassName();
/* Set this default current style for our task */
try {
UIManager.setLookAndFeel( uiName );
} catch ( Exception ex ) {
System.err.printf( "--- Can't set GUI default system style (%s):%s%n", uiName, ex.getMessage() );
}
}
package su.common.crypto;
import org.apache.commons.lang.StringUtils;
import su.common.misc.Text;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;
/**
* Created in su.common.crypto.<br>
* User: ******<br>
* Date: 30.08.2011<br>
* Time: 17:12:45<br>
*/
public class SimpleEncDec {
private static final String DEFAULT_ALGO_NAME = "AES";
// private static final String DEFAULT_CIPHER_NAME = "AES/ECB/PKCS5Padding";
private static final String DEFAULT_CIPHER_NAME = "AES/ECB/NoPadding";
private KeyGenerator m_keygen;
private SecretKey m_key = m_keygen.generateKey();
private Cipher m_cipherIn;
private Cipher m_cipherOut;
public SimpleEncDec( String algorithmName ) throws NoSuchAlgorithmException {
// KeyGenerator m_keygen;
if ( Text.isEmpty( algorithmName ) )
m_keygen = KeyGenerator.getInstance( DEFAULT_ALGO_NAME );
else
m_keygen = KeyGenerator.getInstance( algorithmName );
// Create the cipher
try {
m_cipherIn = Cipher.getInstance( DEFAULT_CIPHER_NAME );
m_cipherIn.init( Cipher.ENCRYPT_MODE, m_key );
m_cipherOut = Cipher.getInstance( DEFAULT_CIPHER_NAME );
m_cipherOut.init( Cipher.DECRYPT_MODE, m_key );
} catch ( Exception e ) {
e.printStackTrace();
}
}
public SimpleEncDec() throws NoSuchAlgorithmException, NoSuchPaddingException {
this( null );
}
public String encrypt( String arg, String password ) {
try {
byte[] res = m_cipherIn.doFinal( arg.getBytes() );
return Base64.encode( res );
} catch ( Exception e ) {
System.err.println(e);
}
return null;
}
public String decrypt( String arg, String password ) {
byte[] res = Base64.decode( arg );
try {
res = m_cipherOut.doFinal( res );
return new String( res );
} catch ( Exception e ) {
System.err.println(e);
}
return null;
}
public static String strPad16( String strToPad ) {
final int len = strToPad.length();
if (len % 16 == 0)
return strToPad;
return StringUtils.rightPad( strToPad, (len + 16 - (len%16)) );
}
public static void main( String[] args ) throws NoSuchAlgorithmException {
SimpleEncDec sed = new SimpleEncDec( "AES" );
// check functionality
final String password = "simple_password";
String checkStr = "String to encode/decode for algorithms test";
System.out.println( String.format( "String to encrypt: \"%s\"", strPad16(checkStr) ) );
// encode string
String encStr = sed.encrypt( strPad16(checkStr), password );
System.out.println(String.format( "Encrypted into \"%s\"", encStr ) );
final String decrypt = sed.decrypt( encStr, password );
String decStr = Text.trimRight( decrypt );
System.out.println( String.format( "Decrypted to \"%s\"", decStr ) );
if ( decStr.equals( checkStr ) )
System.out.println( "+++ Encryption/decryption works well +++" );
else
System.err.println( "--- Encryption/decryption doesn't work ---" );
}
}
package tests;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
/**
* <pre>
* Class name: Demo
* Created by SYGSKY for package ru.ts.tests
* Date: 17.12.2010
* Time: 16:47:55
* <p/>
* ... remove as soon as possible as it is only for testing purposes ...
* <p/>
* Changes:
* </pre>
*/
public class FullScreen extends java.applet.Applet
{
private Label l;
private Window w;
private boolean running;
private int clicks;
private String[] messages = new String[]{
"Прикольно, да?",
"ты хочешь меня... удалить…",
"Ты знаешь, я не должна, но, но...",
"Я прекрасна, прсто кликни по мне ещё раз :)"
};
public synchronized void start()
{
w = new Window( new Frame() );
l = new Label( "PWND" );
l.setFont( new Font( "Serif", Font.BOLD, 120 ) );
l.setAlignment( l.CENTER );
l.setForeground( Color.white );
l.addMouseListener( new MouseAdapter()
{
public void mouseClicked( MouseEvent me )
{
clicked();
}
}
);
l.setCursor( Cursor.getPredefinedCursor( Cursor.HAND_CURSOR ) );
w.setBackground( Color.CYAN );
w.setOpacity( 0.7f );
w.setLayout( new BorderLayout() );
w.add( l, BorderLayout.CENTER );
Dimension ss = Toolkit.getDefaultToolkit().getScreenSize();
w.setBounds( 0, -128, ss.width, ss.height + 256 );
w.setVisible( true );
running = true;
new Thread()
{
public void run()
{
while ( isRunning() )
{
try
{
EventQueue.invokeAndWait( toFront );
sleep( 10 );
}
catch ( Exception ex )
{
ex.printStackTrace();
return;
}
}
}
}.start();
try
{
w.setAlwaysOnTop( true );
}
catch ( Throwable t )
{
// it was just an attempt, we know this should be forbidden to Applets
}
}
private Runnable toFront = new Runnable()
{
public void run()
{
w.toFront();
}
};
private synchronized boolean isRunning()
{
return running;
}
private synchronized void clicked()
{
if ( clicks >= messages.length )
{
running = false;
w.dispose();
return;
}
if ( clicks == 1 )
{
l.setFont( new Font( "Serif", Font.BOLD, 40 ) );
}
l.setText( messages[ clicks++ ] );
}
}
то где писать то, что я писал в main(String args[]). Но если предположить, что ищется метод, где всё инициализируется и код пользователя запускается в первый раз, то это вполне может быть класс в HTTP-сервере (написанном на Java), расширяющий системный класс HTTPServlet и перекрывающий его метод init. Наподобие ниже приведённого:
public final class CustomerDispatcher extends HttpServlet {
...
// Вызывается при инициализации сервлета!
public void init(ServletConfig config) throws ServletException {
super.init(config);
this.context = config.getServletContext();
this.contextPath = this.context.getContextPath();
this.rp = new RequestProcessor(this.context);
this.sman = FssServiceFactory.getFactory().getSecurityManager();
}
...
Date date = new Date();
SimpleDateFormat hoursmins = new SimpleDateFormat("HHmm");
String stringHoursMins = hoursmins .format(date);
int hourandminute = Integer.parseInt(hoursmins );